I am trying to combine multiple streams that comes from Cloud Firestore
as a result of WHERE IN Query. One drawback of this query is that it is limited to only 10 values in one query. But I have list which has more than 10 values so I query every 10 values from that list in one WHERE IN query
and get result as Stream<List<Product>>
. So in the end I want to combine all streams from all query into one Stream<List<Product>>
and return that Stream<List<Product>>
from my function.
This is the code that I am using:
Stream<List<Product>> cartProductsStream(List<List<int>> idGroupList) {
// idGroupList = [[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15]];
// here I am querying from firestore for every list from idGroupList.
for (List<int> listOfTen in idGroupList) {
Stream<List<Product>> resultList = _db
.collection('products')
.where('id', whereIn: listOfTen)
.snapshots()
.map((event) => event.documents
.map((e) => Product.fromJson(
e.data,
))
.toList());
}
}
From here I want to combine every resultList
stream into one stream and return that stream. How can I achieve that? Thanks in advance.
UPDATE
For whom it might interest. With guidance of @pskink and @EdwardLi i solved my problem like this:
my function for streams:
List<Stream<List<Product>>> cartProductsStream(List<List<int>> idGroupList) {
// idGroupList = [[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15]];
List<Stream<List<Product>>> streamList = [];
for (List<int> listOfTen in idGroupList) {
Stream<List<Product>> resultList = _db
.collection('products')
.where('id', whereIn: listOfTen)
.snapshots()
.map((event) => event.documents
.map((e) => Product.fromJson(
e.data,
))
.toList());
streamList.add(resultList);
}
return streamList;
}
my StreamBuilder
widget:
StreamBuilder(
stream: CombineLatestStream.list(stream.cartProductsStream(
idList.toList())), // to combine streamList into one. return type List<List<Product>>
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: LoadingPage(),
);
}
// here snapshot data is List<List<Product>> type. so in this for loop i add all List<Product> into one
for (List<Product> fbList in snapshot.data) {
fbProducts.addAll(fbList.toSet());
}});