Search code examples
flutterblocflutter-bloccubitflutter-cubit

Multi Bloc Provider Flutter


I have several cubits working in my application, but for the cubit to work it always needs to be injected into the MyApp Main of my application, is there any way to concentrate the Cubit / Bloc instances without having to fill the APP with code?

    return MultiBlocProvider(
        providers: [
          BlocProvider(
            create: (context) => AuthenticationCubit(
                secureStorage: secureStorage,
                socialAuthenticationCubit: socialAuthenticationCubit),
          ),
          BlocProvider(
            create: (context) => RegisterAccountCubit(
                secureStorage: secureStorage,
                socialAuthenticationCubit: socialAuthenticationCubit,
                uploadRepository: uploadRepository),
          ),
          BlocProvider(create: (context) => TermsCubit()),
          BlocProvider(create: (context) => EmailVerifyCubit(SecureStorage())),
          BlocProvider(
              create: (context) => AccountProfileCubit(accountRepository,
                  uploadRepository, secureStorage, communityRepository)),
          BlocProvider(
              create: (context) =>
                  ForgotPasswordCubit(authenticationRepository, secureStorage)),
          BlocProvider(create: (context) => PreferenceCubit(SecureStorage())),
        ],
        child: Container(
          child: MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'Inmigra App',
            theme: ThemeData(
              primarySwatch: primaryColor,
            ),
            localizationsDelegates: AppLocalizations.localizationsDelegates,
            supportedLocales: AppLocalizations.supportedLocales,
            locale: _locale,
            home: const SplashScreen(),
            builder: EasyLoading.init(),
          ),
        ));
  }
}

Solution

  • The block mode management uses the inheritance property and you cannot call a block on a page that you have not introduced in the main root. So you must follow the standard structure and always introduce bloc in the main root