错误:找不到正确的提供商< appStateNotifier>在此消费者上方小部件
我尝试将提供程序功能添加到我的应用程序后出现以下错误:
错误:无法在此消费者小部件上方找到正确的提供者
这是我的 ChangeNotifier 代码:
import 'package:flutter/material.dart';
class AppStateNotifier extends ChangeNotifier {
bool isDarkMode = false;
void updateTheme(bool isDarkMode) {
this.isDarkMode = isDarkMode;
notifyListeners();
}
}
并且有我的主类:
import 'package:wilson/src/config/theme_data.dart';
import 'package:wilson/src/routes/index.dart';
import 'package:wilson/src/utils/app_state_notifier.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Wilson extends StatelessWidget {
const Wilson({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Consumer<AppStateNotifier>(builder: (context, appState, child) {
return MaterialApp(
title: 'App-Name',
theme: ThemeConfig.lightTheme,
darkTheme: ThemeConfig.darkTheme,
themeMode: appState.isDarkMode ? ThemeMode.dark : ThemeMode.light,
onGenerateRoute: routes,
);
});
}
}
也许有人可以帮助我,因为我相信这只是一个愚蠢的小错误。
the following error occurs after I tried adding the provider feature to my app:
Error: Could not find the correct Provider above this Consumer Widget
This is my code for the ChangeNotifier:
import 'package:flutter/material.dart';
class AppStateNotifier extends ChangeNotifier {
bool isDarkMode = false;
void updateTheme(bool isDarkMode) {
this.isDarkMode = isDarkMode;
notifyListeners();
}
}
And there is my main class:
import 'package:wilson/src/config/theme_data.dart';
import 'package:wilson/src/routes/index.dart';
import 'package:wilson/src/utils/app_state_notifier.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Wilson extends StatelessWidget {
const Wilson({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Consumer<AppStateNotifier>(builder: (context, appState, child) {
return MaterialApp(
title: 'App-Name',
theme: ThemeConfig.lightTheme,
darkTheme: ThemeConfig.darkTheme,
themeMode: appState.isDarkMode ? ThemeMode.dark : ThemeMode.light,
onGenerateRoute: routes,
);
});
}
}
Maybe someone can help me with that, because I believe it's just a stupid little mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将其初始化到您的主体中。
如果您有多个提供商文件,则可以使用多提供商。
You have to initialize this into your main.dart like this.
You can use multi provider if you have more than one provider files.