Bloc: [Question] When to use Bloc.listen and when to use BlocListener

Created on 7 Apr 2020  ·  3Comments  ·  Source: felangel/bloc

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:

  • what is the difference between the two?
  • when should each be used? will be great if you can provide example

Thanks much

question

Most helpful comment

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.

All 3 comments

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 👍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hivesey picture hivesey  ·  3Comments

wheel1992 picture wheel1992  ·  3Comments

MahdiPishguy picture MahdiPishguy  ·  3Comments

komapeb picture komapeb  ·  3Comments

krusek picture krusek  ·  3Comments