我正在尝试实现一个用于在页面之间进行导航的 Cubit。我的第一次尝试是在我的背景屏幕上实现它,但是在尝试这样做时出现了这个错误:
“错误:在此 BlocBuilder<StateStreamable<Object?>, Object?> Widget 上方找不到正确的 Provider<StateStreamable<Object?>>”
我不明白为什么它没有找到正确的上下文,因为我的 BlocBuilder 在小部件树中的 BlocProvider 之上......
这是代码:
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();
},
);
},
),
);
}
),
);
}
}
有谁知道为什么?:)
感谢您的回答!
克里斯