[英]Two providers in one Widget - Flutter
I have this problem.我有这个问题。 In my App I'm using the Provider package
to manage the login State.在我的应用程序中,我使用Provider package
来管理登录 State。 In the MaterialApp I also want to manage some sort of the user configuration, in this case the Theme selection.在 MaterialApp 中,我还想管理某种用户配置,在本例中是主题选择。
If I try to use two times Provider.of<LoginService>(context)
I'm receiving this error:如果我尝试使用两次Provider.of<LoginService>(context)
我收到此错误:
Could not find the correct Provider<LoginService> above this MyApp Widget
This likely happens because you used a `BuildContext` that does not include the provider
of your choice.
How can I use in Provider more of one time the Provider.of...
or even two different Providers in a Widget (to, for instance, separate my LoginService
and my UserconfigService
)?我如何在 Provider 中多次使用Provider.of...
甚至在一个小部件中使用两个不同的 Providers(例如,将我的LoginService
和我的UserconfigService
)?
Thank you!谢谢!
Actual code:实际代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<LoginService>(
create: (context) => LoginService(),
child: MaterialApp(
title: 'My App',
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
routes: {
'/': (BuildContext context) {
var state = Provider.of<LoginService>(context);
if (state.isLoggedIn()) {
return HomeScreen();
} else {
return LoginScreen();
}
},
MentorScreen.id: (BuildContext context) => MentorScreen(),
},
)
);
}
My objective:我的目标:
child: MaterialApp(
title: 'MyApp',
debugShowCheckedModeBanner: false,
theme: state.isDarkThemeEnabled() == true ? ThemeData.dark() : ThemeData.light(),
...
This type of Error comes when you use the context
just after creating the ChangeNotifierProvider
class.当您在创建ChangeNotifierProvider
class 之后使用context
时,就会出现这种类型的错误。
Similarly, if you use the context
of the Scaffold
to showDialog gives a similar error.同样,如果使用Scaffold
的context
来 showDialog 会给出类似的错误。 Here is the answer that explains why this happens这是解释为什么会发生这种情况的答案
For this Wrap, your MaterialApp
Widget inside Builder
Class which will wait for the class to build first then call the Provider.of<T>(context)
method.对于此包装,您在Builder
Class 中的MaterialApp
Widget 将等待 class 首先构建,然后调用Provider.of<T>(context)
方法。
Builder(
builder: (context) {
return MaterialApp(
title: 'My App',
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
routes: {
'/': (BuildContext context) {
var state = Provider.of<LoginService>(context);
if (state.isLoggedIn()) {
return HomeScreen();
} else {
return LoginScreen();
}
},
MentorScreen.id: (BuildContext context) => MentorScreen(),
},
);
},
),
and for Two Providers in the same widget.对于同一小部件中的两个提供者。
Use MultiProvider
.使用MultiProvider
。
here's code from one of my app.这是我的一个应用程序的代码。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => locator<FAuthService>(),
builder: (context, _) {
return MultiProvider(
child: MaterialApp(
onGenerateRoute: Router.onGenerateRoute,
initialRoute: initialRoute,
navigatorKey: locator<NavigationService>().globalKey,
debugShowCheckedModeBanner: false,
title: 'Demo',
theme: ThemeData(
primaryColor: Colors.black,
),
),
providers: [
ChangeNotifierProvider<HomeVM>(
create: (_) => locator<HomeVM>(),
),
ChangeNotifierProvider<LoginVM>(
create: (context) => locator<LoginVM>(),
),
],
);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.