Flutter: Stream Builder does not work correctly when it is fed with observable1.mergeWith([observable2])

729

I think what the problem here is that each stream emits an event when the response from Firebase arrives, where the data is a collection of entries.

One event example is the result from StreamA is ['a', 'b', 'c'] and
one event from StreamB is ['e', 'f', 'g'].

What you seem to expect from Observable.merge() is

['a', 'b', 'c', 'e', 'f', 'g'],

but actually it emits 2 events.
First

['a', 'b', 'c']

and then

['e', 'f', 'g'].

Flutter/StreamBuilder first causes the data from the first event to render, then, when the 2nd event arrives at the widget, it is rebuilt with the data from the 2nd event.

The Observable.scan operator allows you to combine data from multiple events into a single event.

Share:
729
shubham soni
Author by

shubham soni

Updated on December 05, 2022

Comments

  • shubham soni
    shubham soni over 1 year

    I want to merge two Observables or Streams to be used as a streamed input for A Stream Builder to build build out a listview out of those Streamed objects . .

    I tried

    observable1.mergeWith([observable2])
    

    this but the lsit view only showed observable 2. if i chain another observable like observable1.mergeWith([observable2]).mergewith([observable3]) Or observable1.mergeWith([observable2,observable3])

    this only showed me obervable3.

    I tried concatwith , combinelatest2 but nothing worked. Any Solution to this.

    I tried this too

    Observable.combineLatest2(
                             Observable1, Observable2, (a, b) {
                             print("lengtsdfsdf"+a.length);
                            print("lengtsdfsdf"+b.length);
                            return List.from(a)..addAll(b);
                           }); // Here the Obsevables are of type Observable<List<obj>>