I have this problem. In my App I'm using the Provider package
to manage the login State. In the MaterialApp I also want to manage some sort of the user configuration, in this case the Theme selection.
If I try to use two times Provider.of<LoginService>(context)
I'm receiving this error:
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
)?
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(),
...