Thank you for good library.
I am new in flutter and mobile dev., so sorry for such question.
I need to store global objects (different cached info, data, user settings) and have global methods (utils) in the whole app scope and have access to it from any place of code (any bloc).
How I should do this?
Maybe make static class with static properties and methods or there is a way using repository or data provider? Or make one block and inject in globally?
What app structure to use in this case?
What is the best way you think? And how to use it in your bloc lib approach?
You have no examples for this or I have not found it...
Thank you for great job!
Hi @sergeyplishka 馃憢
It all depends on the type of data you need and whether you only need it globally available or also cached.
For example user details could be made available through a global AuthenticationBloc. Please check out this example for an actual implementation.
If you need to provide some data through a bloc and also cache that data you could make use of HydratedBloc which will automatically cache your state. This is a good choice for something like SettingsBloc.
Hope that helps 馃憤
Thank you for advise.
Where global blocks are stored?
Should it be initialized in build method or BlockProvider will keep it alive?
Do you know where to get examples?
The bloc instance is cached by the BlocProvider between rebuilds(through provider). You should initialize each bloc based on where it's needed, usually at the lowest common ancestor widget of all the sub-trees that need access to it.
You can find a lot of examples here 馃憤
So I can have a lot of blocs and provide it in root widget, then use necessary if I need in any child widget. Are these blocks cached even between switching between application in Android/iOS?
My questing is - can I not worry about necessity to reload all data if user switching between applications?
I wouldn't recommend creating all blocs as global ones, it's better to scope them exactly where it's needed.
Blocs are cached during application lifetime as long as they are not destroyed as a result of navigation or manual dispose, unless we are talking about HydratedBloc which caches between application startups too(aka when you kill an app and you start it again).
Excellent! Thanks for support! This ticket can be closed.
Have a good day, guys.
Most helpful comment
Excellent! Thanks for support! This ticket can be closed.
Have a good day, guys.