[英]Flutter: Connectivity Widget Wrapper Provider error
I've added ConnectivityWrapperWidget with my custom offlineWidget.我已经添加了 ConnectivityWrapperWidget 和我的自定义 offlineWidget。 I'm getting this error, while running the app
运行应用程序时出现此错误
Error: Could not find the correct Provider above this ConnectivityWidgetWrapper Widget错误:在此 ConnectivityWidgetWrapper 小部件上方找不到正确的提供程序
This likely happens because you used a BuildContext
that does not include the provider of your choice.这可能是因为您使用了不包含您选择的提供程序的
BuildContext
。 There are a few common scenarios:有几种常见的场景:
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.您使用了一个
BuildContext
,它是您尝试读取的提供程序的祖先。
Make sure that ConnectivityWidgetWrapper is under your MultiProvider/Provider.确保 ConnectivityWidgetWrapper 在您的 MultiProvider/Provider 下。 This usually happen 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:考虑像这样使用
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>()), } ), }
I don't get it.我不明白。 Can somebody explain, what is the issue
有人可以解释一下,这是什么问题
Try to use builder instead of child, as your error suggests.正如您的错误所暗示的那样,尝试使用 builder 而不是 child。 If it doesn't help, show some code.
如果没有帮助,请显示一些代码。
在添加 ConnectivityWrapper 时,如果使用自定义的 offlineWidget(),则需要将 ConnectivityAppWrapper 添加为 MaterialApp 的父级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.