Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : stream two Streams into a single screen?

I have two streams fetching from two different api.

Stream<Month> get monthOutStream => monthOutController.stream; Stream<MySchedule> get resultOutStream => resultController.stream; 

I am fetching these data at two different state of the application, result at the begining and Months after some events from user.

MyScheduleBloc(){   initialData(); }  Future initialData() async {   MySchedule mySchedule = await myScheduleViewModel.importMySchedule(now.id);   resultController.add(mySchedule); } 

My screen has a streambuilder as

Widget build(BuildContext context) { final webCalenderBloc = WebCalenderBloc(); return StreamBuilder(   stream: webCalenderBloc.resultOutStream,   builder: (BuildContext context , snapdata){     if(!snapdata.hasData){       return Center(         child: CircularProgressIndicator(),       );     }     return body(snapdata.data);    },  ); } 

Since the main widget build method took the StreamBuilder Widget with resultoutstream as a stream.Where do i fetch the other stream monthoutStream. Can i fetch stream inside a stream? Do i missing anything while handling two stream.I dont want to build any widget from monthoutstream but want to check the data inside it.

like image 378
BINAY THAPA MAGAR Avatar asked Aug 16 '18 15:08

BINAY THAPA MAGAR


People also ask

How do I combine two streams in Flutter?

You can just merge two firestore streams directly like: Observable. merge([stream1, stream2]). pipe(combineStream);

How do you combine two streams in Dart?

You can use StreamZip in package:async to combine two streams into one stream of pairs, then create the C objects from that. Note however, StreamZip only emits pairs. If As and Bs emit at different rates, C will wait until they've both emitted a value before emitting a merged pair.

What does StreamBuilder do in Flutter?

The StreamBuilder can listen to exposed streams and return widgets and catch snapshots of got stream information. The stream builder takes two contentions. The Stream resembles a line. At the point when you enter a value from one side and a listener from the opposite side, the listener will get that value.


2 Answers

You can nest StreamBuilder if needed. Nothing prevents you from doing the following:

StreamBuilder(   stream: stream1,   builder: (context, snapshot1) {     return StreamBuilder(       stream: stream2,       builder: (context, snapshot2) {         // do some stuff with both streams here       },     );   }, ) 

Another solution if this makes sense for you is: Streams are designed to be mergeable/transformed. You could make a third stream that is a merge of the two later streams.

Ideally for complex stream operations you'll want to use rxdart as it provides a few useful transformer.

Using rxdart, the fusion of two Observable (which are subclass of Stream) would be the following:

Observable<bool> stream1; Observable<String> stream2;  final fusion = stream1.withLatestFrom(stream2, (foo, bar) {   return MyClass(foo: foo, bar: bar); }); 
like image 94
Rémi Rousselet Avatar answered Sep 28 '22 11:09

Rémi Rousselet


Observable.combineLatest2(         aStream,         bStream,         (a, b, c) =>         a != '' && b != ''); 

combineLatestN returns a combined stream

like image 33
Michel Tonon Avatar answered Sep 28 '22 11:09

Michel Tonon