Bloc: BlocListener's current state is not accessible on v3.2.0

Created on 27 Feb 2020  路  3Comments  路  Source: felangel/bloc

Hi Felix,

Thanks so much for FlutterBloc. I was using v0.13.0 on my project. I faced with a problem when I updated version of package to v3.2.0. I made a sample project.

  1. Run the app, change the step from first to second.
  2. You will see that the last state doesn't come to UI.
  3. Go to pubspec.yaml
  4. Decrease version of flutter_bloc to v0.13.0 and fix the issues which come from version difference. The last state comes to UI at that version.
question

All 3 comments

Hi @cankarabag 馃憢
Thanks for opening an issue!

The difference in behavior is because previously BlocListener was triggered on the initialState whereas in later versions we updated it to only be triggered on state changes (not including the initial state).

I opened a pull request with some suggested fixes. Hope that helps! 馃憤

Why initial state ignored in Listener ? Because we can ignore any state we want in Listener already (With do nothing :) ).

I asked it because want to understand flutter_bloc more deeply.

EDIT: Because sometimes, like this, we need last state (not initial state actually) in bloc listener (for do some things, not build).

@devibrahimkarahan the BlocListener's job is to just listen for changes in the bloc's state. Previously it would always get triggered once with the current bloc state and then again for each subsequent change. This is undesirable because if you were to navigate to the same screen over and over the listener will be triggered even when no state changes have occurred in the bloc.

A simple example of this is having a BlocListener which listens for an error state and shows a SnackBar. Once the bloc is in an error state if the user navigates away and back to this screen they will see a failure SnackBar immediately just because the last state of the bloc was an error state and the BlocListener resubscribed.

Hope that helps!

Was this page helpful?
0 / 5 - 0 ratings