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
Did you try to wrap your
MyApp
inbuilder
?or this line
try accessing the cubit after postFrameCallback, so your initState method would be like this :