Flutter Reloading List with Streams & RxDart
You are not supposed to change the instance of state
.
You should instead submit a new value to the observable. So that StreamBuilder
, which is listening to state
will be notified of a new value.
Which means you can't just have an Observable
instance internally, as Observable
doesn't have any method for adding pushing new values. So you'll need a Subject
.
Basically this changes your Bloc to the following :
class HomeBloc { final Stream<HomeState> state; final EventRepository repository; final Subject<HomeState> _stateSubject; factory HomeBloc(EventRepository respository) { final subject = new BehaviorSubject(seedValue: new HomeState.initial()); return new HomeBloc._( repository: respository, stateSubject: subject, state: subject.asBroadcastStream()); } HomeBloc._({this.state, Subject<HomeState> stateSubject, this.repository}) : _stateSubject = stateSubject; Future<void> loadEvents() async { _stateSubject.add(new HomeState.loading()); try { final list = await repository.getEventList(1); _stateSubject.add(new HomeState(result: list, isLoading: false)); } catch (err) { _stateSubject.addError(err); } }}
Also, notice how loadEvent
use addError
with the exception. Instead of pushing a HomeState
with a hasError: true
.