Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter_bloc 8: what is the best practice for listening to state changes from another bloc

What is the best practice for listening to another bloc’s state changes?

This answer was relevant in previous version, but it doesn’t work in version 8 (.listen method doesn’t exist anymore on a bloc): https://stackoverflow.com/a/62785980/160919

FilteredTodosBloc({@required this.todosBloc}) {
  todosSubscription = todosBloc.listen((state) {
    if (state is TodosLoadSuccess) {
      add(TodosUpdated((todosBloc.state as TodosLoadSuccess).todos));
    }
});}

What is the recommended approach to listen to a state change from another bloc in flutter_bloc 8?

like image 358
a.s.t.r.o Avatar asked Dec 19 '25 02:12

a.s.t.r.o


1 Answers

State stream is now exposed via stream getter, so you can still use almost the same code:

FilteredTodosBloc({required this.todosBloc}) {
  todosSubscription = todosBloc.stream.listen((state) {
    if (state is TodosLoadSuccess) {
      add(TodosUpdated((todosBloc.state as TodosLoadSuccess).todos));
    }
});}
like image 112
Kirill Bubochkin Avatar answered Dec 20 '25 15:12

Kirill Bubochkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!