错误:找不到正确的 Provider上面这个 Practice4HomePage Widget Flutter(BLoc)

发布于 2025-01-09 07:35:19 字数 1006 浏览 3 评论 0原文

在 flutter 中学习 bloc 时出现错误 错误

是 错误:在此 Practice4HomePage 小部件上方找不到正确的提供程序

发生这种情况是因为您使用的 BuildContext 不包含提供程序 由您选择。有一些常见的场景:

  • 您在 main.dart 中添加了一个新的提供程序并执行了热重载。 要修复此问题,请执行热重启。

  • 您尝试读取的提供商位于不同的路线。

    提供者是有“范围的”。因此,如果您在路线中插入提供者,那么 其他路由将无法访问该提供商。

  • 您使用的 BuildContext 是您尝试读取的提供程序的祖先。

    确保 Practice4HomePage 位于您的 MultiProvider/Provider 下。 当您创建提供程序并尝试立即读取它时,通常会发生这种情况。

    例如,而不是:

    小部件构建(BuildContext context){
      返回提供者<示例>(
        创建:(_) =>例子(),
        // 会抛出 ProviderNotFoundError,因为 `context` 是关联的
        // 到作为 `Provider` 父级的小部件
       子:文本(context.watch<示例>()),
      ),
    }
    

    考虑使用builder,如下所示:

    小部件构建(BuildContext context){
      返回提供者<示例>(
        创建:(_) =>例子(),
        // 我们使用 `builder` 来获取一个可以访问提供者的新的 `BuildContext`
        构建器:(上下文){
          // 不再抛出异常
          返回文本(context.watch<示例>()),
        }
      ),
    }
    

I got an error when learning bloc in flutter

Error is
Error: Could not find the correct Provider above this Practice4HomePage 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.

    Make sure that Practice4HomePage is under your MultiProvider/Provider.
    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>()),
      ),
    }
    

    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>()),
        }
      ),
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

坚持沉默 2025-01-16 07:35:19

您必须向您的 Practice4HomePage 类提供该块。一种方法是将块提供给整个小部件树,即使用 BlocProviderMultiBlocProvider 包装 MaterialApp。另一种方法是包装您正在调用的 Practice4HomePageBlocProvider。因此,假设您从 ABC 页面导航到 Practice4HomePage,您可以将 BlocProvider 包装在导航语句中。

例子:

Navigator.of(context).push(
  MaterialPageRoute<HomeForm>(
    builder: (context) => BlocProvider<TimerState>(
      create: (context) => TimerState(),
      child: Practice4HomePage(),
    ),
  ),
),

You have to provide the bloc to your Practice4HomePage class. One way is to provide the bloc to the entire widget tree i.e. wrapping MaterialApp with BlocProvider or MultiBlocProvider. Another way is to wrap BlocProvider which you are calling Practice4HomePage. So let's suppose you are navigating from the ABC page to Practice4HomePage, you can wrap BlocProvider inside your navigation statement.

Example:

Navigator.of(context).push(
  MaterialPageRoute<HomeForm>(
    builder: (context) => BlocProvider<TimerState>(
      create: (context) => TimerState(),
      child: Practice4HomePage(),
    ),
  ),
),
花开柳相依 2025-01-16 07:35:19

首先,您需要使用 BlocProvider 而不是 Provider,其次,使用 Builder 包装您的子窗口小部件,因为 BlocProvider.of(context) (或在本例中为 context.watch())从当前上下文向上查找树以找到您的 BlocProvider,并且您使用与您提供的 Bloc 相同的上下文,因此它无法在树上找到它,因为它位于同一级别(相同的上下文),因此只需使用您的错误提供的建议,或者像这样:

Widget build(BuildContext context) {
  return BlocProvider<Example>(
    create: (_) => Example(),
    child: Builder(builder: (context) => Text(context.watch<Example>())),
  ),
}

或者您可以提取文本将 widget 放入新的 StatelessWidget 中,这也将起作用,因为它将拥有自己的(新的)BuildContext,如下所示:

class ParentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<Example>(
      create: (_) => Example(),
      child: ChildWidget(),
    ),
  }
}

class ChildWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text(context.watch<Example>());
  }
}

Firstly, you need to use BlocProvider instead of Provider, and secondly, wrap your child widget with Builder because BlocProvider.of(context) (or in this case context.watch()) looks from current context up the tree to find your BlocProvider, and you use same context that you provided your Bloc with, so it can't find it up the tree because it's on the same level (same context), so just use suggestion that was provided by your error, or like this:

Widget build(BuildContext context) {
  return BlocProvider<Example>(
    create: (_) => Example(),
    child: Builder(builder: (context) => Text(context.watch<Example>())),
  ),
}

Or you can extract Text widget into a new StatelessWidget and that will also work because it will have it's own (new) BuildContext, like so:

class ParentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<Example>(
      create: (_) => Example(),
      child: ChildWidget(),
    ),
  }
}

class ChildWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text(context.watch<Example>());
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文