找不到提供者 Flutter

发布于 2025-01-17 05:24:08 字数 3011 浏览 3 评论 0原文

我想在不同的小部件之间共享一些数据,因此我决定使用 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 Providers 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2025-01-24 05:24:08

错误消息准确指定并描述了

使用没有 Example 提供程序的 Menu 上下文构建的 ShowMultipleAnswers 错误,因此它抛出此错误。

您可以使用 builder 属性而不是 child 或使用提供程序包装您的 MaterialApp

您的代码应如下所示:

class Menu extends StatefulWidget {
//...SOme code
ChangeNotifierProvider<Example>(
              create: (context) => Example(),
              builder: (context) {
                 return ShowMultipleAnswers();
              } 
//...some code

The error message exactly specifies and describe your error

ShowMultipleAnswers got built using Menu context which doesn't have the Example provider, thus it throws this error.

you can either use builder attribute instead of child or wrap your MaterialApp with the provider

your code should be something like this:

class Menu extends StatefulWidget {
//...SOme code
ChangeNotifierProvider<Example>(
              create: (context) => Example(),
              builder: (context) {
                 return ShowMultipleAnswers();
              } 
//...some code
ゞ花落谁相伴 2025-01-24 05:24:08

我忘了说 MenuShowMultipleAnswers 之间还有一个页面,就像它们之间的桥梁一样,所以真正的流程是:

Menu Page - > 桥接页面 -> 显示多个答案页面

我删除了这个Bridge Page并且它成功了!但我仍然不明白为什么它不起作用,也许是因为 Bridge Page 没有任何其 Provider 的引用?

I forgot to say that there was an other page between Menu and ShowMultipleAnswers, 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 because Bridge Page didn't have any references of its Provider?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文