-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug?] Listener called when being rebuilt, even though no state changes #368
Comments
Fixed by #370 and released in |
Hello @felangel. First of all, thank you for this awesome package! Regarding this issue. You say in the readme file that: |
Maybe I shouldn't put this here but in a project that I'm working on, I was relying on the listener to be called for the |
@biklas7 you can just dispatch the event right when the bloc is created like so: BlocProvider(
builder: (context) => MyBloc()..dispatch(MyBlocEvent()),
child: Container(),
) |
@felangel thank you 👍 Keep up the awesome work on your packages! |
@felangel can you provide some guidance around when to use BlocListener vs BlocBuilder? If "BlocBuilder analogous to StreamBuilder", what is BlocListener analogous to? |
@bobwiller BlocBuilder should only be used to return different widgets based on the bloc state. For things like navigation, showing alerts, showing SnackBars, etc ... you should use BlocListener. This is because BlocBuilder can be called many times by the flutter framework whereas BlocListener is only called once per state change. BlocListener is analogous to manually listening to changes in the bloc state and disposing the subscription. Check out the SnackBar Recipe for more detail 👍 |
@felangel thx - i feel like the warning at the bottom of that recipe ("We should NEVER....") should be in the BlocBuilder documentation and/or have a reference to that recipe in the BlocListener doc page! One last question: I feel like you moved to a pattern where you do: ...when you need a reference to the Bloc. So in that recipe, you make that call twice - once in the Listener and again in the Builder vs. getting the reference in the constructor like this: and just reusing the reference when you need it. I assume there is a reason for this? |
Good point! I’ll make that change 👍 You could get the instance in the build method and reuse the reference in both places. You can’t use BlocProvider.of(context) in the constructor because you need to have access to the BuildContext. The lookup is O(1) so it shouldn’t have any performance impact but I will make that change as well. 👍 |
I think the bug was reintroduced. I just built an app with BlocListener for my navigation. After I experienced some strange behaviour I put a
So my listener function is called twice for each state which really is a problem since my navigation is placed inside that listener some routes get pushed twice within my navigator. My App is currently too complex and I haven't had enough time to investigate everything but it really seems like the bug just somehow was reintroduced. Maybe the Reproduce Part of the original issue already reproduces the issue again (I haven't checked yet). |
@UliPrantz can you put together a sample app which reproduces the issue? Thanks! |
@felangel I try to do it rn. Just could take some time. One simple question that I don't misunderstand the documentation: If my bloc yields the same state twice one after another (with no other state in between) then the listener function is only called once, isn't it? (My actual bloc doesn't do this but it would be easier to write the example app to reproduce the bug) |
@UliPrantz yes, but only if the states can be compared and are evaulated as the same. Check package:equatable if you want to evaulate states with the same data as the same object. 👍 |
@tenhobi ok the thing is I know equatable just a little bit. So the actual State that I'm using is super simple:
and if the function body of my
this would call the listener function only once as far as I understand. |
So I just wrote the app accordingly to my description above and this would call the listener function twice I can also share the github project. (update: this is the git repo for the sample app https://github.com/UliPrantz/TestRepo) |
Link Updated: |
Describe the bug
I have 2 pages with BlocListener in each of them. The Blocs them selves are kept in the
I have a BlocListener on my widget
To Reproduce
Create an app widget with blocs in the state.
Create 2 pages that you can navigate between them
In each page have a BlocListener widget on a state. Make sure the state doesn't change when navigating.
The 'listener' callback will be called every time the BlocListener is rebuilt, even though the state is the same.
Expected behavior
The listener callback in BlocListener should only be called once when the state changes, regardless if the BlocListener was just built or not.
**Logs **
Paste the output of running
flutter doctor -v
here.The text was updated successfully, but these errors were encountered: