How to create a list of groupchats

Viewed 23

I have a firebase document structured as chats/groupchatID/messages/message where GroupchatId is the combination of 2 user Ids such as ID2ID1 or ID1ID2. I have a page that has a listview builder that needs a snapshot of the other user to create the listview for them and to open the groupchats

I currently display the list by displaying all users such as

child: FutureBuilder(
          future: FirebaseFirestore.instance
              .collection('users')
              .where(
                'uid',
                isNotEqualTo: FirebaseAuth.instance.currentUser!.uid,
              )
              .orderBy('uid', descending: true)
              .get(),
          builder: (context, snapshot) {

Im trying to now change it to show only the groupchats that exist in the firebase that the user is in. How can I return only thr groupchats the current user is in like is there a way I can check for substrings in firebase and then would I have to do another firebase query for each list tile to get the data for that user as that seems inefficient

1 Answers

There is no way to check the substring for both users, as Firestore queries can only do prefix filtering: so only strings starting with a certain substring.

The solution is to include an array of the participant UIDs inside the document as an array field, and then filter on that with something like:

.where(
  'participantUIDs',
  arrayContains: FirebaseAuth.instance.currentUser!.uid,
)
Related