in my application there are a couple of pages. two pages need to use one bloc.
I use namedRoutes for navigating between pages and the BlocProvider is on the top of app widget so it is provided for all pages of application but I only want to provide it for two pages so that when I navigate to one of two pages a new instance of bloc is created and when I leave both pages the bloc is disposed...
what can I do?
A very similar question was asked some time ago in the bloc library GitHub issues.
That's what Felix, bloc's creator wrote:
(...) You have 3 options though:
- wrap your
MaterialAppwith aBlocProviderfor global access- use named routes and setup the
BlocProviderto be scoped for the named route- pass the bloc via constructor into the new page and then use
BlocProviderto make it available in the remaining subtree- use your another DI library to provide the blocs
Imo it’s not a big deal to pass the bloc via constructor into the new page and then provide it again (...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With