在此 MyApp 小部件上方找不到正确的提供程序
Could not find the correct provider above this MyApp widget
我正在尝试使用 MaterialApp
中的提供商。我有一个 MultiProvider
,它是 MaterialApp
.
的父级
当我尝试访问提供商时,出现以下错误:
Error: Could not find the correct Provider above this MyApp Widget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<RoutesProvider>(create: (context) => RoutesProvider()),
...
],
child: MaterialApp(
title: 'coolApp',
// key: Provider.of<RoutesProvider>(context, listen: false).mainKey,
initialRoute: '/home',
routes: <String, WidgetBuilder>{...routes},
),
);
}
}
遵循以下结构,ChangeNotifierProvider.value
是分配供应商的正确方式
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: RoutesProvider()),
//.....
],
child: MaterialApp(
我正在尝试使用 MaterialApp
中的提供商。我有一个 MultiProvider
,它是 MaterialApp
.
当我尝试访问提供商时,出现以下错误:
Error: Could not find the correct Provider above this MyApp Widget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<RoutesProvider>(create: (context) => RoutesProvider()),
...
],
child: MaterialApp(
title: 'coolApp',
// key: Provider.of<RoutesProvider>(context, listen: false).mainKey,
initialRoute: '/home',
routes: <String, WidgetBuilder>{...routes},
),
);
}
}
遵循以下结构,ChangeNotifierProvider.value
是分配供应商的正确方式
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: RoutesProvider()),
//.....
],
child: MaterialApp(