Is it possible to combine Firestore streams in Flutter?

26 views Asked by At

In my flutter app, I'm using this Firestore Stream :

  late Stream<QuerySnapshot> requestsClosedOrPending = FirebaseFirestore.instance
      .collection(clients)
      .where('isOpened', isEqualTo: false)
      .where('isPending', isEqualTo: true)
      .snapshots();

So to include the documents in the stream, the document needs to have the field isOpened = false AND isPending = true.

But what I want is to have an OR statement instead : to include the documents in the stream, the document should have the field isOpened = false OR isPending = true. Is it possible ?

Also, I'm using this stream in a StreamBuilder :

  StreamBuilder<QuerySnapshot> requestsStream(Future triggerAnalytics) {
    return StreamBuilder<QuerySnapshot>(
      stream: requestsClosedOrPending,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              // Rest of the code..//

Maybe I should add the or statement in the StreamBuilder itself ? I'm opened to any suggestion that could combine the streams.

1

There are 1 answers

0
Frank van Puffelen On

Firestore nowadays supports having OR conditions in a query, but you got the syntax wrong. Your requirement would translate to something like:

FirebaseFirestore.instance
    .collection(clients)
    .where(
      Filter.or(
        Filter('isOpened', isEqualTo: false),
        Filter('isPending', isEqualTo: true)
      )
    )

Also see the Firestore documentation on using an OR condition in a query.


If you ever find a case where you do need to combine multiple streams, have a look at How do I join data from two Firestore collections in Flutter?