找不到提供者 Flutter
我想在不同的小部件之间共享一些数据,因此我决定使用 ChangeNotifierProvider
及其相对的 Consumer
。我之前已经使用过 Provider ,但从未以这种方式使用过(事实上我遇到了一些错误)。
ChangeNotifierProvider
已在 menu
页面中定义,而 Consumer
已在 menu
中定义的其他小部件中定义也。
菜单页面:
class Menu extends StatefulWidget {
//...SOme code
ChangeNotifierProvider<Example>(
create: (context) => Example(),
child: ShowMultipleAnswers()
//...some code
现在我想在 ShowMultipleAnswers(
) 小部件中使用 Consumer
来消耗菜单中创建的数据,例如:
class ShowMultipleAnswers extends StatefulWidget {
//...some code
Widget build(BuildContext context) {
return Consumer<Example>(builder: (context, handler, child) {
//some code
但我遇到了这些错误:
Error: Could not find the correct Provider<Example> above this Consumer<Example> Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- 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.
consider using `builder` like so:
Make sure that Consumer<Example> is under your MultiProvider/Provider<Example>.
This usually happens when you are creating a provider and trying to read it immediately.
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>()),
),
}
```
```
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>()),
}
),
}
```
我认为最重要的是有效选项是 2 :
- The provider you are trying to read is in a different route.
但我不知道,因为它们应该在同一个中,我的意思是 ShowMultipleAnswers()
是 Provider
子项。 或者第二种
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
在这种情况下,按照上面的建议,我应该使用 builder:(context){}
而不是直接调用 child : ..
但我在提供者之后读到了5 builder
已被 create
取代,所以我很困惑。
如果我使用了错误的小部件,请告诉我!
I want to share some data across different widgets so I decided to use a ChangeNotifierProvider<Example>
with its relative Consumer<Example>
. I have already used Provider
s before but never in this way (in fact I got some errors).
ChangeNotifierProvider<Example>
has been defined in menu
page while Consumer<Example>
in an other widget defined in menu
too.
Menu page :
class Menu extends StatefulWidget {
//...SOme code
ChangeNotifierProvider<Example>(
create: (context) => Example(),
child: ShowMultipleAnswers()
//...some code
And now I would like to use Consumer<Example>
inside ShowMultipleAnswers(
) widget consuming data created in menu like :
class ShowMultipleAnswers extends StatefulWidget {
//...some code
Widget build(BuildContext context) {
return Consumer<Example>(builder: (context, handler, child) {
//some code
But I got these errors :
Error: Could not find the correct Provider<Example> above this Consumer<Example> Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- 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.
consider using `builder` like so:
Make sure that Consumer<Example> is under your MultiProvider/Provider<Example>.
This usually happens when you are creating a provider and trying to read it immediately.
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>()),
),
}
```
```
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>()),
}
),
}
```
I think the most valid options are 2 :
- The provider you are trying to read is in a different route.
But I don't know because they should be in the same, I mean ShowMultipleAnswers()
is the Provider
child.
Or the second
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
In this case, following suggestions above, I should use a builder:(context){}
instead of directly calling child : ..
but I read that after provider 5 builder
has been substituted by create
so I'm confused.
If I'm using wrong widgets tell me please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息准确指定并描述了
使用没有
Example
提供程序的Menu
上下文构建的ShowMultipleAnswers
错误,因此它抛出此错误。您可以使用
builder
属性而不是child
或使用提供程序包装您的MaterialApp
您的代码应如下所示:
The error message exactly specifies and describe your error
ShowMultipleAnswers
got built usingMenu
context which doesn't have theExample
provider, thus it throws this error.you can either use
builder
attribute instead ofchild
or wrap yourMaterialApp
with the provideryour code should be something like this:
我忘了说
Menu
和ShowMultipleAnswers
之间还有一个页面,就像它们之间的桥梁一样,所以真正的流程是:Menu Page
- >桥接页面
->显示多个答案页面
。我删除了这个
Bridge Page
并且它成功了!但我仍然不明白为什么它不起作用,也许是因为Bridge Page
没有任何其Provider
的引用?I forgot to say that there was an other page between
Menu
andShowMultipleAnswers
, like a bridge between them so the real flow was :Menu Page
->Bridge Page
->ShowMultipleAnswers Page
.I removed this
Bridge Page
and it worked! But I still don't understand why it didn't work, maybe becauseBridge Page
didn't have any references of itsProvider
?