felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

Global State to read inside blocs #303

Closed jkyon closed 5 years ago

jkyon commented 5 years ago

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

craiglabenz commented 5 years ago

@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.

felangel commented 5 years ago

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 👍

jkyon commented 5 years ago

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!!!

Sadmansamee commented 4 years ago

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