扑如何显示所有文档的subCollection?

问题描述

我有一个餐厅集合,每个餐厅都有一个订单子集。而且,我试图为所有餐厅的订单制作一个列表视图。在下面的代码中,我只能列出第一家餐厅的订单。我也尝试使用流生成器来执行此操作,但是它不起作用。如果有人帮助我,我会很高兴:)

  Future getOrders() async{
List list_of_restaureants = await Firestore.instance.collection("Restaurants")
    .getDocuments().then((val) => val.documents);
for (int i=0; i<list_of_restaureants.length; i++) {
  QuerySnapshot qn = await Firestore.instance.collection("Restaurants")
      .document(list_of_restaureants[i].documentID.toString()).collection("Orders").getDocuments();
  return qn.documents;
}}

FutureBuilder(
              future: getOrders(),builder: (_,snapshot){
                if(snapshot.connectionState == ConnectionState.waiting){
                  return Center(
                    child: CircularProgressIndicator(),);}
                else{
                  return ListView.builder(
                    itemCount: snapshot.data.length,physics: BouncingScrollPhysics(),itemBuilder: (_,index){
                      return  new ListTile(
                        title: Text(snapshot.data[index].data['phone']),subtitle: Text(snapshot.data[index].data['Address']),);
                    },);
                }
              },),

Database collection

Database subcollection

解决方法

您正在犯一个非常简单的错误。在for循环中,您将添加到列表中,然后将其返回。因此,一旦添加第一个文档,它将立即返回列表,而不会影响for循环。因此,它仅适用于一家餐厅。而是这样做

  Future getOrders() async{
List list_of_restaureants = await Firestore.instance.collection("Restaurants")
    .getDocuments().then((val) => val.documents);
List documentsToReturn;
for (int i=0; i<list_of_restaureants.length; i++) {
  QuerySnapshot qn = await Firestore.instance.collection("Restaurants") .document(list_of_restaureants[i].documentID.toString()).collection("Orders").getDocuments();
  documentsToReturn.addAll(qn.documents);   
    }
  return documentsToReturn;
}
,

对于您的第一个问题,我认为Siddharth Agrawal已经为您提供了正确的答案。

对于第二个问题,您的错误在于以下代码段:

                    title: Text(snapshot.data[index].data['phone']),subtitle: Text(snapshot.data[index].data['Address']),

您宁愿这样访问数据:

 snapshot.data['phone']
 snapshot.data['Address']

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...