Bloc: Is there any downsides when providing all Blocs above MaterialApp?

Created on 21 Nov 2020  路  3Comments  路  Source: felangel/bloc

As the app grows bigger I found myself need to access other bloc's state and data in another bloc more often, and usually their in different route. So I wonder is there any reason to not place all blocs above the MaterialApp widget?

question

All 3 comments

It is fine. You can put it anywhere in the widget tree you want. I do that when I use bloc for dynamic theming or localization, which both are needed in the MaterialApp widget to rebuild it with different them or localization when changed.

Hi @QAQyzk 馃憢
Thanks for opening an issue!

The downsides to providing all blocs globally include:

  • requiring reset events because blocs are never closed (developer experience)
  • subscriptions are long-lived unnecessarily (performance)
  • any part of the app can access any bloc (scoping)

and more.

I would highly recommend scoping blocs to the subtrees that need them and you can use nested Navigators when you want to make a bloc available across multiple routes or use BlocProvider.value to provide an existing bloc to a new route.

I highly recommend checking out the bloc access recipe and https://youtu.be/laqnY0NjU3M for more information.

Hope that helps 馃憤

Thanks! Those information is really helpful! I will keep blocs as scoped as possible. :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tigranhov picture tigranhov  路  3Comments

timtraversy picture timtraversy  路  3Comments

krusek picture krusek  路  3Comments

hivesey picture hivesey  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments