'Get only the groups a user belongs to in Firebase Firestore?

I am trying to get only the groups to which the user belongs, but with the current code I show all the existing groups.

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("My groups"),
        automaticallyImplyLeading: false,
      ),
      body: StreamBuilder(
        stream: userColeccion.doc('$userID').collection("grupos").snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Column(
              children: [
                Text("groups users"),
                Expanded(
                  child: StreamBuilder(
                    stream: FirebaseFirestore.instance
                        .collection("grupos")
                        .snapshots(),
                    builder: (context, groupSnapshot) {
                      if (groupSnapshot.hasData) {
                        var groupList = (groupSnapshot.data
                                as QuerySnapshot<Map<String, dynamic>>)
                            .docs
                            .map((d) => Group.fromJson(d.data()))
                            .toList();

                        return ListView.builder(
                          itemCount: groupList.length,
                          itemBuilder: (context, index) {
                            var group = groupList[index];
                            return Card(
                              child: ListTile(
                                title: Text(group.namegroup),
                              ),
                            );
                          },
                        );
                      }

                      return const Center(child: CircularProgressIndicator());
                    },
                  ),
                ),
              ],
            );
          }

          return const Center(child: CircularProgressIndicator());
        },
      ),
    );
  }
}

when entering "users", you can enter the id of each one, within that id there is a collection called "groups" which stores the key of the different groups to which the user belongs, with this key I intend to search in the "groups" collection that is at the "users" level, but it stores the key of the groups that exist.

collection "usuarios"

enter image description here

enter image description here

collection "grupos"

enter image description here

In short, I would like to show on the screen only the groups to which the user belongs, the keys are stored by the user, thank you very much for any help or explanation.



Solution 1:[1]

To get the documents from grupos where namegroup is equal to Grupo 2, you can use a query:

stream: userColeccion.doc('$userID').collection("grupos")
  .where("namegroup", "==", "Grupo 2")
  .snapshots(),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen