错误:找不到正确的 Provider上面这个 Practice4HomePage Widget Flutter(BLoc)
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须向您的
Practice4HomePage
类提供该块。一种方法是将块提供给整个小部件树,即使用BlocProvider
或MultiBlocProvider
包装MaterialApp
。另一种方法是包装您正在调用的Practice4HomePage
的BlocProvider
。因此,假设您从 ABC 页面导航到Practice4HomePage
,您可以将BlocProvider
包装在导航语句中。例子:
You have to provide the bloc to your
Practice4HomePage
class. One way is to provide the bloc to the entire widget tree i.e. wrappingMaterialApp
withBlocProvider
orMultiBlocProvider
. Another way is to wrapBlocProvider
which you are callingPractice4HomePage
. So let's suppose you are navigating from the ABC page toPractice4HomePage
, you can wrapBlocProvider
inside your navigation statement.Example:
首先,您需要使用 BlocProvider 而不是 Provider,其次,使用 Builder 包装您的子窗口小部件,因为 BlocProvider.of(context) (或在本例中为 context.watch())从当前上下文向上查找树以找到您的 BlocProvider,并且您使用与您提供的 Bloc 相同的上下文,因此它无法在树上找到它,因为它位于同一级别(相同的上下文),因此只需使用您的错误提供的建议,或者像这样:
或者您可以提取文本将 widget 放入新的 StatelessWidget 中,这也将起作用,因为它将拥有自己的(新的)BuildContext,如下所示:
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:
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: