[英]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.