最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - ProviderNotFoundException when using multiple cubit in the MultiBlocProvider - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Jan 18 at 12:12 Arun XenaArun Xena 744 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Did you try to wrap your MyApp in builder?

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

or this line

home: Builder(builder: (_) => const SplashScreen()),

try accessing the cubit after postFrameCallback, so your initState method would be like this :

initState(){
WidgetsBinding.instance!.addPostFrameCallback((_) {
 final bloc= context.read<YourBloc>();
})

} 
发布评论

评论列表(0)

  1. 暂无评论