I'm trying to use a provider in MaterialApp
. I have a MultiProvider
which is a parent of the MaterialApp
.
When I try accessing the provider, I get the following error:
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},
),
);
}
}
Follow the below structure,
ChangeNotifierProvider.value
is the right way of assigning provider