I'm trying to implements a Cubit that is used to make the navigation bewteen the pages. My first try was to implement it on my background screens, but I got this error when trying to do it :
"Error: Could not find the correct Provider<StateStreamable<Object?>> above this BlocBuilder<StateStreamable<Object?>, Object?> Widget"
I don't understand why it doesn't find the correct context because my BlocBuilder is above the BlocProvider in the widget tree ...
Here's the code :
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flexmes_mobile_app/buisness_logic/cubit/internet_cubit.dart';
import 'package:flexmes_mobile_app/buisness_logic/utility/app_bloc_observer.dart';
import 'package:flexmes_mobile_app/config/themes.dart';
import 'package:flexmes_mobile_app/ui/screens/auth_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:responsive_sizer/responsive_sizer.dart';
import 'buisness_logic/cubit/navigation_cubit.dart';
import 'config/app_router.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp();
BlocOverrides.runZoned(
() => runApp(MyApp()),
blocObserver: AppBlocObserver(),
);
}
class MyApp extends StatelessWidget {
final AppRouter _appRouter = AppRouter();
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MediaQuery(
data: const MediaQueryData(),
child: ResponsiveSizer(
builder: (context, orientation, deviceType) {
return MultiBlocProvider(
providers: [
BlocProvider<InternetCubit>(
create: (context) => InternetCubit(connectivity: Connectivity()),
),
BlocProvider<NavigationCubit>(
create: (context) => NavigationCubit(),
),
],
child: MaterialApp(
title: 'Flexmes Mobile App',
//Generate routes for navigation
onGenerateRoute: _appRouter.generateRoute,
//Take the correct theme to apply to the screens
theme: appThemeData[AppTheme.authTheme],
//Build the default widget, the "background" widget
builder: (context, child) {
//Instantiate the Cubits
BlocProvider.of<InternetCubit>(context);
BlocProvider.of<NavigationCubit>(context);
return BlocBuilder(
builder: (BuildContext context, state) {
if (state is AuthenticationInitial){
return AuthScreen(child: child);
}
return Container();
},
);
},
),
);
}
),
);
}
}
Does anyone knows why ? :)
Thanks for your answers !
Chris