我正在使用Flutter和二手“提供商”开发聊天应用程序。包裹。但是我得到这个错误
错误:无法在此明信片窗口上找到正确的提供商
,因为您使用了A <代码> buildContext 不包括提供商 您的选择。有几种常见的情况:
您在
main.dart
中添加了一个新的提供商,并执行了热线功能。 要修复,请执行热点。您要阅读的提供商是在不同的路线中。
提供商“范围”。因此,如果您将提供商插入路线中,则 其他路线将无法访问该提供商。
您使用了
buildContext
,它是您要阅读的提供者的祖先。确保明信片位于您的多个载体/提供商下。 通常,当您创建提供商并立即阅读它时,这通常会发生。
例如,而不是:
widget build(buildContext上下文){ 返回提供商&lt;示例&gt;( 创建:(_)=&gt;例子(), //将抛出一个providernotfounderror,因为“上下文”是关联的 //到``提供者&lt; example&gt;'' 儿童:text(context.watch&lt; example&gt;()。toString()), ); }
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论