问题
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