平面列表为空,数组中包含值

时间:2019-04-28 10:05:58

标签: react-native google-cloud-firestore

我用querySnapshot填充了一个数组,但列表列表未呈现任何内容

试图更改renderItem代码

constructor(props) {
  super(props);
  var rootref = firebase.firestore().collection("deneme");
  var wholeData = []
  rootref.get().then(function(querySnapshot){
      querySnapshot.forEach(function(doc) {
        wholeData.push(doc.data())
      });
  });
};

render() {
  return (
    <View>
      <FlatList
         data={this.wholeData}
         renderItem={({item}) => <Text>{item.isim}, {item.soyisim}</Text>}
      />
    </View>
  );
};

1 个答案:

答案 0 :(得分:2)

您将必须使用setState来通知组件数据已更改。更改您的代码以执行以下操作:

 constructor(props) {
      super(props);

      this.state = {
       data: []
      };

      var rootref = firebase.firestore().collection("deneme");
      rootref.get().then(querySnapshot => 
       const wholeData = querySnapshot.map(doc => doc.data()));

       // notify your component that the data has changed
       this.setState({
        data: wholeData
       })
    };

 render() {
  return (
    <View>
      <FlatList
         data={this.state.data} // get your data from the state
         renderItem={({item}) => <Text>{item.isim}, {item.soyisim}</Text>}
      />
    </View>
  );

这样,一旦您收到了WholeData,FlatList就会更新。