Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide a bloc for only two pages of application?

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?

like image 322
Mahdi Arabpour Avatar asked Dec 06 '25 08:12

Mahdi Arabpour


1 Answers

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 MaterialApp with a BlocProvider for global access
  • use named routes and setup the BlocProvider to be scoped for the named route
  • pass the bloc via constructor into the new page and then use BlocProvider to 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 (...)

  • https://github.com/felangel/bloc/issues/74#issuecomment-497308365
like image 138
Albert221 Avatar answered Dec 08 '25 00:12

Albert221



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!