没有为类查询定义“ get”方法

时间:2020-01-01 12:00:47

标签: firebase flutter dart google-cloud-firestore

错误:没有为类查询定义“ get”方法

我对如何使用查询从Firestore中获取数据以及如何在文本中显示数据了解不多,请提供建议

getData(String docId)async{
  final  QuerySnapshot snapshot = await Firestore.instance.collection('listofprods').where('id', isEqualTo: docId).get(); // error in the get
  return snapshot;
  }
}

在futurebuilder中也出现错误,当我使用getdocument()而不是get()时,方法[]被调用为null

FutureBuilder(
                future: fd.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()));
                      },
                    ),
                  );
                },
         ),

2 个答案:

答案 0 :(得分:2)

要获取单个文档,您不应该查询数据库,而应该获取它;)

await Firestore.instance.collection('listofprods').doc(docId).get();

futureBuilder应该如下所示:

FutureBuilder(
                future: Firestore.instance.collection('listofprods').doc(docId).get(),
                builder: (BuildContext context, DocumentSnapshot 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()));
                      },
                    ),
                  );
                },
         ),

顺便说一句,您可以将Firestore.instance.collection('listofprods').doc(docId).get()扩展为另一个函数, 但无需使其异步,而是返回Future本身(不要添加等待状态)。

答案 1 :(得分:0)

在FutureBuilder中,您必须检查数据是否来自数据库。您可以使用snapshot.hasData

FutureBuilder(
                future: Firestore.instance.collection('listofprods').doc(docId).get(),
                builder: (BuildContext context, DocumentSnapshot snapshot) {
                  if(snapshot.hasData)
                   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()));
                      },
                    ),
                  );
                  else
                     return CircularProgressIndicator();
                },
         ),

但是此过程是错误的,因为如果我们要获取一个固定的文档,则应该使用DocumentSnapshot而不是QuerySnapshot。并且应该使用StreamBuilder而不是FutureBuilder