简体   繁体   中英

Flutter: Connectivity Widget Wrapper Provider error

I've added ConnectivityWrapperWidget with my custom offlineWidget. I'm getting this error, while running the app

Error: Could not find the correct Provider above this ConnectivityWidgetWrapper Widget

This likely happens because you used a BuildContext that does not include the provider of your choice. 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.

    Make sure that ConnectivityWidgetWrapper is under your 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:

     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. If it doesn't help, show some code.

在添加 ConnectivityWrapper 时,如果使用自定义的 offlineWidget(),则需要将 ConnectivityAppWrapper 添加为 MaterialApp 的父级。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM