Reputation: 51
A a = await createA();
...
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ChangeNotifierProvider<A>.value(
value: a,
child: const NewPage(),
),
),
);
When I try to use object 'a' as below inside the build function of the NewPage, I am getting below error. Anything wrong with what am I doing?
context.watch<A>()
Error: Could not find the correct Provider above this NewPage Widget
This happens because you used a BuildContext that does not include the provider of your choice. There are a few common scenarios:
Upvotes: 1
Views: 541
Reputation: 1413
It is because you are using child
in provider, which means the widget won't build upon the provider.
You should use builder:
ChangeNotifierProvider<A>.value(
value: a,
builder: (context, child) => const NewPage(),
);
Now the provider are able to inject the dependency(A
), and rebuild your desired widget(NewPage
) with the correct context.
Upvotes: 1