skip to Main Content

I’ve been using cubit for my flutter project.

This is how my main.dart looks like,

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // Set system preferences before running the app
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
  ));

  runApp(MultiBlocProvider(providers: [
    BlocProvider<ThemeCubit>(
        lazy: false,
        create: (themeContext) => ThemeCubit(ThemePreferences())..loadTheme()),
    BlocProvider<WalletInfoCubit>(
        lazy: false, create: (walletInfoContext) => WalletInfoCubit()),
    BlocProvider<SplashCubit>(
        lazy: false, create: (splashContext) => SplashCubit()),
    BlocProvider<StartupScriptsCubit>(
        lazy: false, create: (startupContext) => StartupScriptsCubit()),
    BlocProvider<ImportWalletCubit>(
        lazy: false, create: (importwalletContext) => ImportWalletCubit()),
    BlocProvider<CreateWalletCubit>(
        lazy: false, create: (createwalletContext) => CreateWalletCubit()),
    BlocProvider<HomePageCubit>(
        lazy: false, create: (homePageContext) => HomePageCubit()),
    BlocProvider<ImportTokenCubit>(
        lazy: false, create: (importTokenContext) => ImportTokenCubit()),
    BlocProvider<TempCubit>(lazy: false, create: (tempcontext) => TempCubit()),
  ], child: const MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ThemeCubit, ThemeCubitState>(
        builder: (themecubitCntext, state) {
      return ScreenUtilInit(
          designSize: const Size(375, 812),
          splitScreenMode: true,
          builder: (screenutilContext, child) {
            return MaterialApp(
              navigatorKey: navigatorKey,
              debugShowCheckedModeBanner: false,
              builder: (materialAppcontext, child) {
                return MediaQuery(
                  data: MediaQuery.of(materialAppcontext)
                      .copyWith(textScaler: const TextScaler.linear(1.0)),
                  child: child!,
                );
              },
              title: 'My app',
              theme: state.isDark ? AppTheme.dark : AppTheme.light,
              home: const SplashScreen(),
            );
          });
    });
  }
}

All the cubits can be accessed on Splash Screen except for WalletInfo cubit,

final walletCubit = BlocProvider.of(context);

This line in Splash Screen gives me the error,
ProviderNotFoundException (Error: Could not find the correct Provider<WalletInfoCubit> above this SplashScreen Widget.

I’ve tried all the solutions suggested by chatgpt, but don’t seem to work, can anyone help me please?

NOTE : The cubit was called in initstate(), in build method, and in onDependencyChanged() as well, but the context won’t recognize the just WalletInfo Cubit. Every other cubit in the MultiBloc Provider works

2

Answers


  1. Did you try to wrap your MyApp in builder?

    child: Builder(builder: MyApp()) ...
    

    or this line

    home: Builder(builder: (_) => const SplashScreen()),
    
    Login or Signup to reply.
  2. try accessing the cubit after postFrameCallback, so your initState method would be like this :

    initState(){
    WidgetsBinding.instance!.addPostFrameCallback((_) {
     final bloc= context.read<YourBloc>();
    })
    
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search