简体   繁体   中英

Could not find the correct Provider<>

I have a class that makes an HTTP get request and two screens the first one is displayed titles that fetched from API the second screen displays the posts that also fetched from the API and all using provider What I did is that in the main class home:

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

and in the two screens, I did that final

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

but this error appears

Error: Could not find the correct Provider above this Discription Widget flutter: flutter: To fix, please: flutter: flutter: * Ensure the Provider is an ancestor to this Discription Widget flutter: * Provide types to Provider flutter: * Provide types to Consumer flutter: * Provide types to Provider.of() flutter: * Always use package imports. Ex: import 'package:my_app/my_code.dart'; flutter: * Ensure the
correctcontext` is being used.

how to solve it????

ok, I solve it by wrapping the ChangeNotifierProvider into a material widget instead of the home page so the entire application could access the provider

 return ChangeNotifierProvider<NewsRequest>(
      builder: (_) => NewsRequest(),
          child: 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