Flutter BLOC and Provider how to register them together

Viewed 1731

I'm writing an app using BLOC architecture and registered bloc providers like this in the main.dart:

runApp(MultiBlocProvider(providers: [
    BlocProvider<OrderBloc>(
      create: (context) {
        return OrderBloc()..add(OrderInitialEvent());
      },
    ),
    BlocProvider<AuthenticationBloc>(
      create: (context) {
        return AuthenticationBloc(userService: userService)..add(AppStarted());
      },
    ),
    ...
   ], child: MyApp()));

Now I need to use Provider approach along with BLOC but not sure how to register it? Is it possible? Thanks

1 Answers

You can just nest them:

runApp(MultiBlocProvider(providers: [
    BlocProvider<OrderBloc>(
      create: (context) {
        return OrderBloc()..add(OrderInitialEvent());
      },
    ),
    BlocProvider<AuthenticationBloc>(
      create: (context) {
        return AuthenticationBloc(userService: userService)..add(AppStarted());
      },
    ),
    ...
   ], 
child: 
MultiProvider(
  providers: [
    Provider<Something>(create: (_) => Something()),
    Provider<SomethingElse>(create: (_) => SomethingElse()),
    Provider<AnotherThing>(create: (_) => AnotherThing()),
  ],
  child: MyApp(),
)));
Related