Reputation: 109
I am trying to make a query in firebase, but to bring all the information I need, I have to put two streams within the Stream builder.
return StreamBuilder<QuerySnapshot>(
//Here I need to place two Streams, how could I do it
stream: partnerRequest,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (_, index) {
Upvotes: 3
Views: 2609
Reputation: 2017
You can use rxdart package, It provides function CombineLatestStream which will allow you to combine multiple streams.
Here is an example.
The following example combines 2 streams ie. stream1 and stream2 with help of combiner function.
CombineLatestStream.combine2(
stream1,
stream2,
(s1, s2) => CombinerFunction(...),
)
Here is a more detailed example
Let's say we have 2 streams A and B and both stream boolean value. Now we want our streamBuilder to output some data only when both the values are true or else show and error.
StreamBuilder<bool>(
stream : CombineLatestStream.combine2(A,B,(a,b)=>a&b),
builder:(context,snapshot){
if(snapshot.hasData && snapshot.data == true)
return Text("Successful");
else
return Text("Some error");
}
);
Now CombineLatestStream will only output data when there is data from both the streams. So once both the streams have data it will perform the combiner function(and function for our example) and output the result. Hence we get True only when both the stream A and B have True value and false if any stream is having False value
Hope this helps!
Upvotes: 3
Reputation: 1512
You can use two stream builder as messy way such as below. But remember to change snapshot names to avoid confuse.
StreamBuilder(
stream: stream1,
builder: (context, snapshot) {
return StreamBuilder(
stream: stream2,
builder: (context, snapshot2) {
Upvotes: 1