Bloc: Global State to read inside blocs

Created on 17 May 2019  路  4Comments  路  Source: felangel/bloc

Hi
First of all, I want to say thank you for this amazing library which is easier to use and build clean architecture, so we decide to migrate our app to bloc.

Im now migrating my app from redux to bloc, and I found a problem with some global states that I had in redux such as Internet Connectivity. I had a service which uploads the connectivity status when it changes (for instance, turn off the internet in the device). The status is now in a global state and it is updated by my service and I used it y several places in my app.

With bloc I can麓t use my service in the same way I did with redux.

Is there any way to use global state in bloc or any workaround?

Thank you in advance

question

Most helpful comment

Thank you so much @craiglabenz and @felangel, your suggestion works perfectly. I just create a bloc for may connectivity events, and put ir on my bloc provider. Now I can use the current connection anywhere馃憤馃憤馃憤馃憤

Thank you!!!

All 4 comments

@jkyon - Lots of the author's sample apps demo BlocProvider widgets at the very top - even wrapping the MaterialApp() definition itself. Using this pattern, you should be able to grab that global state, like InternetConnectivity, from any descending widget.

Hi @jkyon 馃憢
Thanks for opening an issue and for the positive feedback! We really appreciate you giving the bloc library a try 馃挴

As @craiglabenz mentioned, you should be able to use a BlocProvider (for a single bloc) or BlocProviderTree (for multiple blocs) and wrap your entire MaterialApp in order to make the blocs globally available.

Hope that helps and let us know if you have any additional questions 馃憤

Thank you so much @craiglabenz and @felangel, your suggestion works perfectly. I just create a bloc for may connectivity events, and put ir on my bloc provider. Now I can use the current connection anywhere馃憤馃憤馃憤馃憤

Thank you!!!

@jkyon would you like to share on how you wrote the bloc?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricktotec picture ricktotec  路  3Comments

nhwilly picture nhwilly  路  3Comments

wheel1992 picture wheel1992  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments

Reidond picture Reidond  路  3Comments