Reputation: 806
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????
Upvotes: 5
Views: 3676
Reputation: 806
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(.....)
Upvotes: 3