Using flutter_bloc 0.5.2
At the top level of my app I have a custom BlocProvider which is very similar to yours, but gives me access to two of my blocs (i) AppDataBloc (ii) AnalyticsBloc.
My view is wrapped in your BlockBuilder and it passes in the main AppDataBloc bloc as the bloc parameter.
Inside my view it uses AppDataBloc to load initial data and on scrolling (infinite scroll). Other actions on that page may cause an event for AnalyticsBloc to be dispatched.
This seems to work most of the time, but I have noticed that some of the AnalyticsBloc events get delayed until some other action is performed e.g. AnalyticsBloc::mapEventToState isn't executed until I change to a different view or trigger the infinite scroll on the main view.
Looking in the debugger I can see that all events have been correctly dispatched and that the processing is delayed.
Is this because the BlockBuilder only knows about the stream from AppDataBloc and so it doesn't cause a flutter render when AppDataBloc is changed? This was my initial thought, but it doesn't explain why it frequently works.
Should I be using two different blocs like this? Should I be able to dispatch an event to any other unrelated bloc from inside BlockBuilder?
@rowanwork you should definitely be able to dispatch events to as many blocs as you want.
Is your AnalyticsBloc overriding transform and debouncing? If so, remove the transform override altogether and you should get the behavior you are expecting.
Let me know if that helps.
@rowanwork closing for now but feel free to let me know if you're still having trouble and I'll reopen this.
My main AppDataBloc had a debounce, but I have removed this an it doesn't make a difference. It seems to be more pronounced when running in "hot reload" via VS Code, could this be interfering with it?
@rowanwork there must be some other problem. Hot reload should have no impact. Are you able to share the code?
I can't share the code as this stage. I will try to reduce it to a simple example which exhibits the same behaviour.
Hi @felangel !!
Is it possible to access blocs in scopes? Like I want to reuse a TeamListBloc in multiple pages but I don't want to place it as a parent of MaterialApp widget.
If yes, could you please share some reference links or any sample code.
Hey @TheHemantKaushik!
If you have different pages that you are pushing (Navigator.push(...)) you will need to either inject the bloc into the page via the constructor or add a BlocProvider above MaterialApp. This is because of the way BuildContext works in Flutter.
If pages aren't pushed using the Navigator then you can use a BlocProvider in the lowest common ancestor widget in the tree.
Let me know if that helps! 馃憤
I found that difficult, might be I'm exceptional case 馃構. That is the reason I moved to rx_command this time. I'm using get_it to inject and get the dependencies. May be it will help me in near future 馃榾.
That鈥檚 totally fine! There are many ways to accomplish it and there鈥檚 no right or wrong way. 馃憤
Most helpful comment
Hey @TheHemantKaushik!
If you have different pages that you are pushing (
Navigator.push(...)) you will need to either inject the bloc into the page via the constructor or add a BlocProvider aboveMaterialApp. This is because of the wayBuildContextworks in Flutter.If pages aren't pushed using the
Navigatorthen you can use aBlocProviderin the lowest common ancestor widget in the tree.Let me know if that helps! 馃憤