Bloc: How to Provide Bloc to other screens

Created on 26 Nov 2020  ยท  16Comments  ยท  Source: felangel/bloc

Hi

in my widget init code i have the following:
@override
void initState() {
_authErrorHandler = AuthErrorHandler();
_authBloc = AuthBloc();
super.initState();
}

i would like to make it such that the bloc (_authBloc) is available for all screens in the path that comes after the current screen (including the current screen), my goal is to be able to get reference to the bloc using BlocProvide.of ...

i couldn't figure out a way, can you pleas help?

question

All 16 comments

Hi @pazhut ๐Ÿ‘‹

You can use a BlocProvider to provide your auth bloc on top of the MaterialApp, that way your bloc will be globally available to any route/screen. Read more about it here.

Hi

I am aware of the possibility to add bloc as global, but i doesn't want to
use it, i would like to have the bloc available only to part of the
navigation path, not the whole app, can I do that?

On Fri, Nov 27, 2020, 2:24 PM Felix Angelov notifications@github.com
wrote:

Closed #1969 https://github.com/felangel/bloc/issues/1969.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#event-4047127564, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGKW4BH3SR2I2GFJEB3SR74FPANCNFSM4UEGEXWA
.

Hi

do you have any information to my last comment? in general i thought that i can use BlocProvider.value to inject the block on the navigation tree whenever needed?

@pazhut check out the Bloc Access Recipe ๐Ÿ‘

Hi

i am not sure i managed to find an answer to my question in the recipe,
what i am looking for is:

  • screenA (which is not the first screen in the app) create bloc => blocA =
    BlocA();
  • screenA push screenB, screenB do not need access to thee bloc
  • screenB push screenC - which need access to the bloc (lets say add event
    so this will be reflected when we pop all the way to screenA)
  • when screenA is being popped the bloc will be closed as we do not need it
    any longer

Can you please point me to which part of your recipe covers this use case?
Thanks much

On Tue, Dec 1, 2020 at 12:02 PM Felix Angelov notifications@github.com
wrote:

@pazhut https://github.com/pazhut check out the Bloc Access Recipe
https://bloclibrary.dev/#/recipesflutterblocaccess?id=recipes-bloc-access
๐Ÿ‘

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#issuecomment-736685447, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGJNT22XYXEY5LPLYPDSSUOSNANCNFSM4UEGEXWA
.

@pazhut how are you pushing routes? Are you using Navigator.push or Navigator.pushNamed?

i am using Navigator.pushNamed

On Tue, Dec 1, 2020 at 12:43 PM Felix Angelov notifications@github.com
wrote:

@pazhut https://github.com/pazhut how are you pushing routes? Are you
using Navigator.push or Navigator.pushNamed?

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#issuecomment-736711445, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGJJABIAXCLMIJ77SLLSSUTM5ANCNFSM4UEGEXWA
.

Did you read through the Named Route Access section?

That example allocates the bloc globally - so not achieving my goal of the
bloc being released after ScreenA popped.

maybe what i should use is: Generated Route Access? and make sure i pass
the correct bloc to the correct screen?

On Tue, Dec 1, 2020 at 1:16 PM Felix Angelov notifications@github.com
wrote:

Did you read through the Named Route Access
https://bloclibrary.dev/#/recipesflutterblocaccess?id=named-route-access
section?

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#issuecomment-736729776, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGIEE3CORM2PFRO7XG3SSUXGZANCNFSM4UEGEXWA
.

@pazhut you can use a nested Navigator so it doesn't have to be a global bloc. Yeah you also use generated route access as well ๐Ÿ‘

You can achieve your desired setup by simply using BlocProvider.value and passing the bloc to route A and route C only, or a nested navigator.

You can achieve your desired setup by simply using BlocProvider.value and
passing the bloc to route A and route C only - can you provide me with an
example on how to do that?

On Tue, Dec 1, 2020 at 1:39 PM Rolly Peres notifications@github.com wrote:

You can achieve your desired setup by simply using BlocProvider.value and
passing the bloc to route A and route C only, or a nested navigator.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#issuecomment-736742340, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGNYN5PWXDW6PNFMVBDSSUZ6ZANCNFSM4UEGEXWA
.

class AppRouter {
  final _counterBloc = CounterBloc();

  Route onGenerateRoute(RouteSettings settings) {
    switch (settings.name) {
      case 'routeA':
        return MaterialPageRoute(
          builder: (_) => BlocProvider.value(
            value: _counterBloc,
            child: PageA(),
          ),
        );
      case '/routeC':
        return MaterialPageRoute(
          builder: (_) => BlocProvider.value(
            value: _counterBloc,
            child: PageC(),
          ),
        );
      default:
        return null;
    }
  }

  void dispose() {
    _counterBloc.close();
  }
}

OK thanks got it, only that it uses push and not pushNamed :)

On Tue, Dec 1, 2020 at 1:46 PM Felix Angelov notifications@github.com
wrote:

@pazhut https://github.com/pazhut refer to
https://bloclibrary.dev/#/recipesflutterblocaccess?id=homepage

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#issuecomment-736745736, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGMCZDEUHQ5ZI3RJUM3SSU2YBANCNFSM4UEGEXWA
.

thanks much for your support guys

On Tue, Dec 1, 2020 at 1:48 PM Rolly Peres notifications@github.com wrote:

class AppRouter {
final _counterBloc = CounterBloc();

Route onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case 'routeA':
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _counterBloc,
child: PageA(),
),
);
case '/routeC':
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _counterBloc,
child: PageC(),
),
);
default:
return null;
}
}

void dispose() {
_counterBloc.close();
}
}

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/1969#issuecomment-736747067, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/ANFZIGO7YDUPMA4AWQOEQLDSSU3BTANCNFSM4UEGEXWA
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  ยท  3Comments

RobPFarley picture RobPFarley  ยท  3Comments

abinvp picture abinvp  ยท  3Comments

clicksocial picture clicksocial  ยท  3Comments

1AlexFix1 picture 1AlexFix1  ยท  3Comments