Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Multiple Firestore Queries

I am trying to make multiple queries to Firestore and merge the results into one stream like here. I tried using StreamGroup.merge() but it's only returning the results of one stream. I noticed that it does actually get data for all the streams but only returns one when everything completes. Here is what I did:

Stream getStream(){
    List<Stream> streams = [];     

    streams.add(Firestore.instance.collection(Constants.REQUESTS_NODE).
    where("municipality",isEqualTo: "City of Johannesburg Metropolitan").
    where("service_id",isEqualTo: 2).
    snapshots());
    streams.add(Firestore.instance.collection(Constants.REQUESTS_NODE).
    where("municipality",isEqualTo: "Lesedi Local").where("service_id",isEqualTo: 2).
    snapshots());    

    return StreamGroup.merge(streams);

  }

What am I missing and doing wrong? The reason I'm doing this is to compensate for Firestore's lack of OR operator.

like image 771
spongyboss Avatar asked Sep 05 '18 11:09

spongyboss


1 Answers

I found this online, hope it helps:

`import 'package:async/async.dart';

Stream<DocumentSnapshot> stream1 = firestore.document('/path1').snapshots();
Stream<DocumentSnapshot> stream2 = firestore.document('/path2').snapshots();
StreamZip bothStreams = StreamZip([stream1, stream2]);

// To use stream
bothStreams.listen((snaps) {
 DocumentSnapshot snapshot1 = snaps[0];
 DocumentSnapshot snapshot2 = snaps[1];

 // ...
});`
like image 194
Mohammed Avatar answered Sep 30 '22 18:09

Mohammed