簡體   English   中英

找不到正確的Provider <>

[英]Could not find the correct Provider<>

我有一個發出HTTP獲取請求的類,並且有兩個屏幕,第一個屏幕顯示從API獲取的標題,第二個屏幕顯示從API獲取的帖子,所有這些都使用提供程序,我在主類主頁中所做的就是:

ChangeNotifierProvider<NewsRequest>(
 child:News(title: "Bitcoin News",),
 builder: (_) => NewsRequest() ),

在兩個屏幕中,我做了最后一個

 newsResponse = Provider.of<NewsRequest>(context);

但出現此錯誤

錯誤:在此Discription Widget顫動之上找不到正確的提供程序:flutter:要進行修復,請:Flutter:flutter:*確保提供者是該Discription Widget顫動的祖先:*向Provider Flutter提供類型:*向消費者提供類型flutter:*為Provider.of()提供類型flutter:*始終使用包導入。 例如:import'package:my_app / my_code.dart'; 顫動:*確保
正在使用correctcontext`。

如何解決呢????

好的,我通過將ChangeNotifierProvider包裝到材料小部件而不是首頁中來解決此問題,以便整個應用程序都可以訪問該提供程序

 return ChangeNotifierProvider<NewsRequest>(
      builder: (_) => NewsRequest(),
          child: MaterialApp(.....)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM