4

I have final _fetcher = PublishSubject<MyModel>() ; in my bloc Component. Here is structure of MyModel:

MyModel { List<MyObjects> _objects = []; 
List<MyObjects> get allObjects => _objects; }

also there is

Observable<MyModel> get myObjects => _fetcher.stream;

in bloc.

I have two pages, first displays list of MyObjects inside Listview.builder, and second displays selected MyObject data.
I'm trying to get data from myObjects using StreamBuilder.
In the first page all objects displays perfectly. But when I open a page with selected object, my AsyncSnapshot inside StreamBuilder always has connections.state waiting, although I have data in stream.
What am I doing wrong?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

1 Answers1

6

Having data doesn't mean you always have access to it.

By default streams (and subjects) don't store the data they received earlier. So if you're coming late to the party, then sorry for you but no data.

To solve this problem rxdart introduces a ReplaySubject and BehaviorSubject. Both are used so that late listeners can still grab the last few events. ReplaySubject will keep track of the N latest, while BehaviorSubject will keep only the last one.

Using a BehaviorSubject instead of PublishSubject should do the trick

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432