Luke
Luke

Reputation: 37

Is there a Flutter/Dart equivalent for the IN query on a Cloud Firestore Firebase Collection ref?

I want to query a Firebase collection reference with a list of multiple document IDs within a Streambuilder, but it appears the only

.where()

query functions available are:

Query where(String field, {dynamic isEqualTo, dynamic isLessThan, dynamic isLessThanOrEqualTo, dynamic isGreaterThan, dynamic isGreaterThanOrEqualTo, dynamic arrayContains, bool isNull})

I have tested isEqualTo and this works fine if I set a string to pass in, but I can't get this to work with a list. It appears there is an IN function in the official Firebase documentation which would provide this function, to a maximum of 10 items:

https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any

Is there an equivalent IN function in Flutter or another way to achieve this? The list I am trying to pass in is actually served by a parent Streambuilder, which queries a User collection for the specific user and passes in all groups to the second Streambuilder:

class _StarredChatState extends State<StarredChat> {

final _firestore = Firestore.instance;
  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    final userID = user.uid;   
    return StreamBuilder<QuerySnapshot>( 
      stream: _firestore.collection('users/$userID/chats')
                        .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
        List<String> ChatIDList = [];
        final ChatIDs = snapshot.data.documents;
        for(var x in ChatIDs) {
          final ChatID = x.data['chats'].toString();
          ChatIDList.add(ChatID);
        }

        return StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('chatrooms')
                            // here is where I need the IN operator as neither
                            // arraycontains or isEqualTo give the desired result
                            .where('description', arrayContains: ChatIDList)
                            .snapshots(),
          builder: (context, snapshot) {

          if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
          //Here is where I want to return a ListBuilder for the above returned 
          //Streambuilder list

          }

          );


      }
    );
  }
}

My other attempt has been to include all users who have joined a chatroom as a subcollection within the individual chat document, but as these have unique autoID's I'm not able to generate a stream of all chats with my specific user as I can't pass in a full path to the nested subcollection. Any other pointers would be really appreciated.

Thanks

Upvotes: 1

Views: 511

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

My guess is that you're on an older version of FlutterFire's cloud_firestore plugin, as the whereIn operator is definitely present in the current version of the library. See the reference docs here.

You might want to upgrade to the latest version of FlutterFire, to get the operator.

Upvotes: 1

Related Questions