felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

How to Provide Bloc to other screens #1969

Closed pazhut closed 3 years ago

pazhut commented 3 years ago

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?

narcodico commented 3 years ago

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.

pazhut commented 3 years ago

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 .

pazhut commented 3 years ago

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?

felangel commented 3 years ago

@pazhut check out the Bloc Access Recipe 👍

pazhut commented 3 years ago

Hi

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

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 .

felangel commented 3 years ago

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

pazhut commented 3 years ago

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 .

felangel commented 3 years ago

Did you read through the Named Route Access section?

pazhut commented 3 years ago

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 .

felangel commented 3 years ago

@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 👍

narcodico commented 3 years ago

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.

pazhut commented 3 years ago

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 .

felangel commented 3 years ago

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

narcodico commented 3 years ago
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();
  }
}
pazhut commented 3 years ago

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 .

pazhut commented 3 years ago

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 .