This happens when instantiating the NotificationsPage routed from the MyHomePage()
Here is my code :
void main() =>runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
final db = AppDatabase();
return MultiProvider(
providers: [
// ignore: unnecessary_statements, missing_return
Provider<MeetingDao>( create: (BuildContext context) {builder: (_) => db.Table1Dao;}, ),
// ignore: unnecessary_statements, missing_return
Provider<UserDao>( create: (BuildContext context) {builder: (_) => db.Table2Dao;}, ),
],
child: MaterialApp(
title: 'MyApp',
home: MyHomePage(),
),
);
}
}
Here is the error log:
Error: Could not find the correct Provider above this NotificationsPage Widget
This likely happens because you used a BuildContext
that does not include the provider
of your choice. There are a few common scenarios:
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.
You used a
BuildContext
that is an ancestor of the provider you are trying to read.Make sure that NotificationsPage is under your MultiProvider/Provider. This usually happen when you are creating a provider and trying to read it immediatly.
For example, instead of:
Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // Will throw a ProviderNotFoundError, because `context` is associated // to the widget that is the parent of `Provider<Example>` child: Text(context.watch<Example>()), ), }
consider using
builder
like so:Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // we use `builder` to obtain a new `BuildContext` that has access to the provider builder: (context) { // No longer throws return Text(context.watch<Example>()), } ), }
Any suggestion on whats wrong with this main page ? in the Notifications Page I am accessing it like
final tbl1Dao = Provider.of<Table1Dao>(context,listen: false);