【问题标题】:Flatlist is empty and array has values in itFlatlist 为空且数组中有值
【发布时间】:2019-04-28 10:05:58
【问题描述】:

我用 querySnapshot 填充了一个数组,但 flatlist 没有呈现任何内容

试图更改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>
  );
};

【问题讨论】:

    标签: react-native google-cloud-firestore


    【解决方案1】:

    您必须使用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 就会更新。

    【讨论】:

    • 谢谢,但现在我遇到另一个错误 querySnapshot is not a function。
    • querySnapshot 只是一个对象。如果您错误地将其作为函数调用,则应检查您的代码。
    • 我复制了整个代码。 const 部分给出错误。
    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 2018-12-21
    • 2021-11-09
    • 2021-01-24
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多