Flutter Multiple Firestore Queries

心已入冬 提交于 2020-05-13 04:36:51

问题


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.


回答1:


For anyone in future who might need assistance with this, I managed to solve it using StreamZip. You can read more about it from this website




回答2:


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];

 // ...
});`



回答3:


You can use whereIn condition, it's a List<dynamic>, that's work with me!

For example:

Firestore db = Firestore.instance;
db.collection
.where("status", whereIn: ["available", "unavailable", "busy"])
.getDocuments();


来源:https://stackoverflow.com/questions/52183978/flutter-multiple-firestore-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!