0

I am trying to query my Firebase database.

enter image description here

I wrote the following code to pull the list of groups where a specific user is a member. The code is appended below. When I run the code, it returns nothing. I tried to query directly using query builder in the console, however, it does not work there either.

I have been at this for sometime but cannot seem to find the issue. Can someone help me out?

Widget _buildGroupsList() {
  return StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance
        .collection('groups')
        .where(
          'members',
          arrayContainsAny: [
            {'id': user.uid}
          ],
        )
        .snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) {
        return const Center(child: Text('Something went wrong'));
      }


      if (snapshot.connectionState == ConnectionState.waiting) {
        return const Center(child: CircularProgressIndicator());
      }
      final List<Widget> groupCards = [];

      for (var doc in snapshot.data!.docs) {
        Map<String, dynamic> groupData = doc.data() as Map<String, dynamic>;
        groupCards.add(_buildGroupCard(groupData));
      }

      return ListView(
        children: groupCards,
      );
    },
  );
}

I built the query in the console and it did not return the data either. It did not throw and error. It just did not return anything.

2
  • 1
    It's not possible with Firestore to filter documents using just a single property of an object in an array. You must know the entire contents of an object in order to filter it from an array. If you want to do this query, you will need a different document structure, perhaps one that has a dedicated array with only user IDs that you can filter with an array-contains query. Commented Dec 22, 2024 at 13:52
  • Thank you. I ended up adding an array field to the groups collection to store the list of ids for all members in the group. This allows me to use the arrayContains. Commented Dec 23, 2024 at 19:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.