I'm obviously misunderstanding how StreamGroup.merge works or how stream works or both!. I have two streams that are querying the same field in firestore. And I'm using merge to make one stream called mergedStream. This is used in a streamBuilder to serve up to the app.
Here's my code:
Stream<List<Order>> stream1({Order order}) {
return _service.collectionStream(
path: APIPath.orders(),
queryBuilder: (query) =>
query.where('orderStatus', isEqualTo: 'Pending'),
builder: (documentSnapshot) => Order.fromFirebase(documentSnapshot));
}
Stream<List<Order>> stream2({Order order}) {
return _service.collectionStream(
path: APIPath.orders(),
queryBuilder: (query) =>
query.where('orderStatus', isEqualTo: 'Preparing'),
builder: (documentSnapshot) => Order.fromFirebase(documentSnapshot));
}
and the merged stream I use for my stream builder:
Stream<List<Order>> mergedStream() {
final s1 = preparingStream();
final s2 = pendingStream();
return StreamGroup.merge([s2, s1]);
}
Switching the order [s2, s1] changes the stream shown. And as the page loads I momentarily see the other stream but it disappears and the other stream appears..How can I merge the streams...?
EDIT: I'm adding the flutter tag because reading around it might be a problem with StreamBuilder
EDIT: This has helped a bit:(link in comments, And was just a proof of concept that the streams merged) But the stream searchResult() doesn't update when there's a state changes in one of he streams...You have to refresh the browser. (Flutter web)
Stream<List<Order>> searchResult() {
List<Stream<List<Order>>> streamList = [stream1(), stream2()];
var x = Rx.merge(streamList).scan<List<Order>>((acc, curr,i) {
return acc ?? <Order>[]
..addAll(curr);
});
//this test shows that the stream gets contents from both query streams
// x.map((convert){ convert.documents.forEach((f){print(f.data["name"]);});}).listen(print);
return x;
}