Flutter : Multiple Cubits Example

247

you can use MultiBlocProvider for cubits and you will not have any problems.

MultiBlocProvider(
    providers: [
        BlocProvider(
            create: (BuildContext context) => MyCubit())
        ], 
    child: Container())

you will probably need Bloc-to-Bloc Communication too

these bloc example projects, which use multiple blocs and cubits in a project that may help you. flutter_todos, flutter_shopping_cart, flutter_firebase_login

Share:
247
Turkey
Author by

Turkey

Updated on November 29, 2022

Comments

  • Turkey
    Turkey over 1 year

    I'm using Cubits for state management in my Flutter app.
    It is my favourite state-management approach so far.

    However, I now want to get a little more complex, and have multiple cubits, controlling different bits of state each (eg. one is for login-related state, another for config/settings state, and another for the main app state).

    I'm trying to find an example of how to do multiple cubits and I'm not finding anything.

    With the BLoc approach we would use MultiBlocProvider.

    Is there an equivalent to the MultiBlocProvider for cubits?
    Or, can you point me at a tutorial that demonstrates using multiple cubits in the one app?

    • lava
      lava about 2 years
      pub.dev/documentation/flutter_cubit/latestMultiCubitProvider‌​( providers: [ CubitProvider<CubitA>( create: (BuildContext context) => CubitA(), ), CubitProvider<CubitB>( create: (BuildContext context) => CubitB(), ), CubitProvider<CubitC>( create: (BuildContext context) => CubitC(), ), ], child: ChildA(), )
  • Turkey
    Turkey about 2 years
    Thank you - and, yes, my own research told me that should work - I haven't got back to implementing it yet, but once I have and confirm it works I'll mark this as the correct answer.