Hi
this is not a bug but a question, i am using this library for few month now and finding it very useful, so thanks a lot.
i still have some issues understanding the difference between Bloc.listen and the BlocListener widget, it seems that both basically performing the same function: listen to a bloc are react on State change, so my questions are:
Thanks much
Hey @pazhut,
Bloc.listen should be used when you're using multiple Blocs interacting with each other, or if one 'Bloc' depends on another. For example, if operations of BlocA depends on the state of BlocB, you can attach a listener to BlocB from BlocA. This returns a StreamSubscription of State of BlocB, which BlocA needs to cancel at some point in time (say when closing BlocA).
BlocListener is a widget which can be used to perform one-time actions like showing Snackbar, pushing new route on the navigator, etc. You can try doing the same thing using Bloc.listen too by embedding it inside some widget's build method. But, you might have problems with gracefully closing the subscription that Bloc.listen returns, because you need to handle the Widget lifecycle then. So, instead of using Bloc.listen for modifying the UI, you can use BlocListener which disposes itself properly when necessary.
Sorry that I couldn't provide code examples since I'm posting this from a mobile device. Hope that helped.
thanks, that is helpful
Paz
On Tue, Apr 7, 2020 at 3:42 AM theDarkBoffin notifications@github.com
wrote:
Hey @pazhut https://github.com/pazhut,
Bloc.listen should be used when you're using multiple Blocs interactive
between themselves. For example, if operations of BlocA depends on the
state of BlocB, you can attach a listener to BlocB from BlocA. This
returns a StreamSubscription of State of BlocB, which BlocA needs to
cancel at some point of time (say when closing BlocA).BlocListener is a widget which can be used to perform one-time actions
like showing Snackbar, pushing new route on navigator etc. You can
obviously try doing the same thing using Bloc.listen too by embedding it
inside some widget's build method. But, you might have problems with
gracefully closing the subscription that Bloc.listen returns, because you
need to handle Widget lifecycle then. So, instead of using Bloc.listen
for modifying the UI, you can use BlocListener which disposes itself
properly when necessary.Sorry that I couldn't provide code examples since I'm posting this from
mobile device. Hope that helped.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1009#issuecomment-610230289, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGLQN7ASW4LPJM3DFRTRLLKPZANCNFSM4MCX6QUA
.
Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍
Most helpful comment
Hey @pazhut,
Bloc.listenshould be used when you're using multiple Blocs interacting with each other, or if one 'Bloc' depends on another. For example, if operations ofBlocAdepends on the state ofBlocB, you can attach a listener toBlocBfromBlocA. This returns aStreamSubscriptionofStateofBlocB, whichBlocAneeds to cancel at some point in time (say when closingBlocA).BlocListeneris a widget which can be used to perform one-time actions like showing Snackbar, pushing new route on the navigator, etc. You can try doing the same thing usingBloc.listentoo by embedding it inside some widget's build method. But, you might have problems with gracefully closing the subscription thatBloc.listenreturns, because you need to handle the Widget lifecycle then. So, instead of usingBloc.listenfor modifying the UI, you can useBlocListenerwhich disposes itself properly when necessary.Sorry that I couldn't provide code examples since I'm posting this from a mobile device. Hope that helped.