[英]Flutter Error: Could not find the correct Provider<User> above this Wrapper Widget
[英]Flutter: Connectivity Widget Wrapper Provider error
我已经添加了 ConnectivityWrapperWidget 和我的自定义 offlineWidget。 运行应用程序时出现此错误
错误:在此 ConnectivityWidgetWrapper 小部件上方找不到正确的提供程序
这可能是因为您使用了不包含您选择的提供程序的BuildContext
。 有几种常见的场景:
您尝试读取的提供程序位于不同的路径中。
提供者是“有范围的”。 因此,如果您在路由中插入提供者,则其他路由将无法访问该提供者。
您使用了一个BuildContext
,它是您尝试读取的提供程序的祖先。
确保 ConnectivityWidgetWrapper 在您的 MultiProvider/Provider 下。 这通常发生在您创建提供程序并尝试立即读取它时。
例如,而不是:
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>()), ), }
考虑像这样使用builder
:
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>()), } ), }
我不明白。 有人可以解释一下,这是什么问题
正如您的错误所暗示的那样,尝试使用 builder 而不是 child。 如果没有帮助,请显示一些代码。
在添加 ConnectivityWrapper 时,如果使用自定义的 offlineWidget(),则需要将 ConnectivityAppWrapper 添加为 MaterialApp 的父级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.