flutterfirebasegoogle-cloud-firestore

Flutter Firebase Query: whereIn filter returning 0 posts


I have the code

QuerySnapshot snapshot = await FirebaseFirestore.instance
        .collection('Posts')
        .where('groupId', whereIn: [
          'oVF05dlEdzS3vdj3TvSG'
        ]) // Using whereIn for multiple groups
        .orderBy('createdAtTimestamp', descending: true)
        .startAfterDocument(_lastDocument!)
        .limit(_numberOfPosts)
        .get();

This query worked fine before i added this .where filter looking at groupIds.

I then added a groupId filter and used a group as an example. I currently have one Post document that has the groupId as you can see with the exact same id. Why is it returning 0 posts? Is there something I'm missing?

enter image description here

There will only be 5 groups max so its ok.


Solution

  • The whereIn clause checks if a single-value field has one of multiple possible values.

    Your groupId is an array field, so you need to use arrayContains:

    .where('groupId', arrayContains: 'oVF05dlEdzS3vdj3TvSG')
    

    Also see the Firebase documentation on querying by array membership.