问题是,当我尝试导航到屏幕时,会收到错误消息,显示“索引1处的绑定值为空”。该错误没有指向代码中的任何一行,而是说问题与sqlite和asyncstorage有关。
我不确定为什么会发生这种情况,但可能与generatedData状态未正确初始化有关。
我不知道该怎么做才能解决这个问题,任何帮助都将不胜感激。
以下是代码:
import React, { Component } from 'react';
import {
Text,
View,
TouchableOpacity,
} from 'react-native';
class DataApp extends Component {
constructor() {
super()
this.state = {
dataLocalIds: [
"data1Data",
"data2Data",
"data3Data",
"data4Data",
"data5Data",
"data6Data"
],
dataLabel: [
"[ data 1 ]",
"[ data 2 ]",
"[ data 3 ]",
"[ data 4 ]",
"[ data 5 ]",
"[ data 6 ]",
],
generatedData: [],
}
this.state.dataLocalIds.map((value, index) => {
this.data = this.getDatas(value);
})
}
async getDatas(value) {
try {
const value = await AsyncStorage.getItem(value).then(val => {
let jsonInit = {};
jsonInit[value] = value;
let newArray = this.state.generatedData;
newArray.push(jsonInit);
this.setState(generatedData: newArray);
return JSON.parse(val)
});
return value
} catch (err) {
throw err
}
}
renderScreen = () => {
var myData = JSON.parse(this.state.generatedData);
return (
{this.state.dataLabel.map((item, index) => {
return(
<TouchableOpacity onPress={}>
<Text>{this.state.generatedData[index] === "{}" ? item : "foo: " + generatedData.foo + " / bar: " + generatedData.bar}</Text>
</TouchableOpacity>
)
})}
);
}
render() {
return (
this.renderScreen()
);
}
}
export default DataApp;