【问题标题】:how to read data from a Querysnapshot如何从 Querysnapshot 中读取数据
【发布时间】:2020-01-01 12:35:40
【问题描述】:

我想从 Querysnapshot 中读取数据。我使用futurebuilder,但出现错误:

方法 [] 在 null 上被调用

小部件

FutureBuilder(
  future: getData('ac1'),
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black)
      ),
      child: ListTile(
        title: Text(snapshot.data['name'].toString()),
        trailing: Text(snapshot.data['icon'].toString()),
        onTap: (){
          Navigator.push(context, MaterialPageRoute(builder: (context) => Ac()));
        },
      ),
    );
  },
),

功能

getData(String docId)async{
  final  QuerySnapshot snapshot = await Firestore
    .instance.collection('listofprods')
    .where('id', isEqualTo: docId)
    .getDocuments();

  return snapshot;
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:
    getData(String docId) async {
      final QuerySnapshot snapshot = await Firestore.instance.collection(
          'listofprods').where('id', isEqualTo: docId).getDocuments();
      return snapshot;
    }
    return FutureBuilder(
      future: getData('ac1'),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasData) {
          return Container(
            decoration:
            BoxDecoration(border: Border.all(color: Colors.black)),
            child: ListTile(
              title: Text(snapshot.data.documents[0].toString()),
              trailing: Text(snapshot.data.documents[0]['name'].toString()),
              onTap: () {
                Navigator.push(
                    context, MaterialPageRoute(builder: (context) => Ac()));
              },
            ),
          );
        } else {
          return `Your widget
        `
      }
    
      },
    );
    

    您需要从文档中获取数据

    【讨论】:

      【解决方案2】:

      快照并不总是有数据,因此您需要检查快照有数据的情况,例如,像这样修改可能会起作用

                   FutureBuilder(
                      future: getData('ac1'),
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                       if(!snapshot.hasData || snapshot.hasError) return CircularProgressIndicator(); //line added
                       if(snapshot.hasData) //line added
                        return Container(
                          decoration:
                              BoxDecoration(border: Border.all(color: Colors.black)),
                          child: ListTile(
                            title: Text(snapshot.data['name'].toString()),
                            trailing: Text(snapshot.data['icon'].toString()),
                            onTap: (){
                              Navigator.push(context, MaterialPageRoute(builder: (context) => Ac()));
                            },
                          ),
                        );
                      },
               ),
      
      
      
      
       getData(String docId)async{
        //if this docId is the document id you cannot use where('id', isEqualTo: docId) to get document of that id instead use following
        final  DocumentSnapshot snapshot = await Firestore.instance.collection('listofprods').document(docId).get();
        return snapshot;
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-27
        • 1970-01-01
        • 2020-09-07
        • 2021-08-02
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多