如何在streambuilder中查询firestore文档?

时间:2020-05-11 16:26:06

标签: firebase flutter google-cloud-firestore

我正在尝试从一个名为“ users / uid”的Firestore集合中检索组,其中包含每个用户的groupID。 我想查询名为“ groups”的集合,并从名为“ user / uid / usergroups”的集合中检索与groupID匹配的文档。

这是我的第一种方法:

child: SafeArea(
                child: StreamBuilder<QuerySnapshot>(
                    stream: userGroupIDs(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (!snapshot.hasData) {
                        return new Text('Connecting...',style: TextStyle(fontSize: 80),);
                      } else {return ListView.builder(
                          itemCount: snapshot.data.documents.length,
                          itemBuilder: (context, i) {
                            DocumentSnapshot groups =
                            snapshot.data.documents[i];
                            return GruppenEintrag(
                              groups['Groupname'],
                                  () => deleteGroup(groups['GroupID']),
                              groups['Creator'],
                              uid,
                            );
                          });
                      }
                    })  
            ),
          ),
        ),
      ),
    );
  }
  Stream<QuerySnapshot>  userGroupIDs() async*{
    String docId;
    Stream<QuerySnapshot> snap = Firestore.instance.collection('users').document(uid).collection('usergroups').snapshots();
    await for(var snapData in snap ){
      snapData.documents.forEach((docResult){
        docId =  docResult.documentID;
        print(docId);
      });
    }
    yield* Firestore.instance.collection('groups').where('GroupID', isEqualTo: docId).snapshots();
  }

enter image description here

enter image description here

0 个答案:

没有答案