0

I have two main collections:

  • groups, which is keyed on auto-ids, and has GroupID = (auto-id), Creator and Groupname fields.
  • users, which is keyed on uid, and has a subcollection usergroups which contains all GroupIDs (auto-ids from the Groups collection) that are assigned to the user.

Now I want to filter the groups collection with the usergroups collection.

The result should be that the user only sees the groups that are saved in his user profile. enter image description here

enter image description here

My first attempt looks like this:

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;
       });
     }
   yield* Firestore.instance.collection('groups').where('GroupID', isEqualTo: docId).snapshots();
  }

Then inside StreamBuilder:

StreamBuilder<QuerySnapshot>(
                            stream: userGroupIDs(),
Fabian840
  • 27
  • 6
  • do you want to filter groups which is inside user collection ? in userGroups from while collection's GroupId do you want to use filter ? – Viren V Varasadiya May 12 '20 at 13:07
  • Your setup looks promising already. You'll want to: 1) get a list of group IDs, instead of the single `docId` you now keep, and then: 2a) either pass that list to a [`whereIn` query](https://stackoverflow.com/a/60046386), 2b) or perform a single-document lookup for each doc ID. Also see my somewhat related answer here: https://stackoverflow.com/a/59097364 – Frank van Puffelen May 12 '20 at 13:21
  • @VirenVVarasadiya It's hard for me to explain. I want to match the documents ids in the "usergroups" with the documents who have the same id in the "groups" collection. Only the matching documents should be displayed in a listview. Sorry my english is not that good. – Fabian840 May 12 '20 at 13:24

0 Answers0