错误:<ThemeNotifier>在此视频小部件上方找不到正确的提供程序



当我试图打开一个新的main.dart时,我会遇到这个错误。例如:我需要从main.dart打开main2.dart,但仍然显示Provider(ThemeNotifier(。

main.dart

MenuItem(
title: "Click".tr(),
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Click())
);
},
),
MenuItem(
title: "Extra".tr(),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyApp2()),
);
},
),

这是我的主要2.dart

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Wakelock.enable();
await Firebase.initializeApp(
// options: DefaultFirebaseOptions.currentPlatform,
);
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
await Hive.initFlutter();
await Hive.openBox(authBox);
await Hive.openBox(userDetailsBox);
await Hive.openBox(settingsBox);
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness:
DesignConfig.isDark ? Brightness.light : Brightness.dark,
// status bar color
));
final pushNotificationService = PushNotificationService(firebaseMessaging);
//pushNotificationService.initialise();
runApp(ChangeNotifierProvider<ThemeNotifier>(
create: (BuildContext context) {
String theme = SettingsLocalDataSource().theme();
if (theme == StringRes.darkThemeKey) {
ISDARK = "true";
} else {
ISDARK = "false";
}
var brightness = SchedulerBinding.instance.window.platformBrightness;
ISDARK = (brightness == Brightness.dark).toString();
return ThemeNotifier(theme == StringRes.lightThemeKey
? ThemeMode.light
: ThemeMode.dark);
},
child: const MyApp2()));
}
class MyApp2 extends StatefulWidget {
const MyApp2({Key key}) : super(key: key);
@override
State<MyApp2> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp2> {
@override
Widget build(BuildContext context) {
final themeNotifier = Provider.of<ThemeNotifier>(context);
return Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
child: MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Provider1(),
),
ChangeNotifierProvider(
create: (_) => Provider2(),
),
ChangeNotifierProvider(
create: (_) => Provider3(),
),
ChangeNotifierProvider(
create: (_) => Provider4(),
),
ChangeNotifierProvider(
create: (_) => Provider5(),
),
],
child: MaterialApp(
builder: (context, widget) {
return Directionality(
textDirection:
TextDirection.ltr, // set here your text direction
child: ScrollConfiguration(
behavior: GlobalScrollBehavior(), child: widget));
},
title: appName,
theme: ThemeData(
shadowColor: primaryColor.withOpacity(0.25),
brightness: Brightness.light,
primaryColor: primaryColor,
scaffoldBackgroundColor: scaffoldBackgroundColor,
backgroundColor: backgroundColor,
canvasColor: blackColor,
hintColor: iconHintColor,
),
darkTheme: ThemeData(
shadowColor: darkPrimaryColor.withOpacity(0.25),
brightness: Brightness.dark,
primaryColor: darkPrimaryColor,
scaffoldBackgroundColor: darkScaffoldBackgroundColor,
backgroundColor: darkBackgroundColor,
canvasColor: darkBlackColor,
hintColor: darkIconHint,

),
themeMode: themeNotifier.getThemeMode(),
initialRoute: Routes.splash,
onGenerateRoute: Routes.onGenerateRoute,
)));
}
}

但当我试图打开一个main2.dart时,错误就会显示出来。发生这种情况是因为您使用了不包括提供程序的BuildContext由您选择。有几个常见的场景:

  • 您在main.dart中添加了一个新的提供程序,并执行了热重新加载。若要修复此问题,请执行热重新启动。

  • 您试图读取的提供程序处于不同的路径。

    提供者是";scoped";。因此,如果在路由中插入提供者,那么其他路由将无法访问该提供商。

  • 您使用了BuildContext,它是您尝试读取的提供程序的祖先。

    确保VideosYou在您的MultiProvider/Provider下。当您创建一个提供程序并尝试立即读取它时,通常会发生这种情况。

main函数是应用程序的入口点。当推送新路由时,只实例化MyApp2,不调用main

最新更新