我正在使用Flutter和二手“提供商”开发聊天应用程序。包裹。但是我得到这个错误

发布于 2025-02-12 07:49:21 字数 736 浏览 0 评论 0原文

代码

错误:无法在此明信片窗口上找到正确的提供商

,因为您使用了A <代码> buildContext 不包括提供商 您的选择。有几种常见的情况:

  • 您在main.dart中添加了一个新的提供商,并执行了热线功能。 要修复,请执行热点。

  • 您要阅读的提供商是在不同的路线中。

    提供商“范围”。因此,如果您将提供商插入路线中,则 其他路线将无法访问该提供商。

  • 您使用了buildContext,它是您要阅读的提供者的祖先。

    确保明信片位于您的多个载体/提供商下。 通常,当您创建提供商并立即阅读它时,这通常会发生。

    例如,而不是:

      widget build(buildContext上下文){
      返回提供商&lt;示例&gt;(
        创建:(_)=&gt;例子(),
        //将抛出一个providernotfounderror,因为“上下文”是关联的
        //到``提供者&lt; example&gt;''
        儿童:text(context.watch&lt; example&gt;()。toString()),
      );
    }
     

Code

Error: Could not find the correct Provider above this PostCard 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 PostCard 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>().toString()),
      );
    }
    

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文