I am trying to understand Flutter Riverpod. I can fetch and display data from the service and understand the benefits of using AsyncValue. However when I try to call the requestWeather function inside a StateNotifier which should call the retrieveWeather in turn and get the weather information for a new location. But I get this error.
The method 'requestWeather' isn't defined for the type 'AsyncValue'. Try correcting the name to the name of an existing method, or defining a method named 'requestWeather'.dartundefined_method
From main.dart where I am trying to access the requestWeather function using Provider.
class InputLocationBox extends ConsumerWidget {
String result = "";
// final weatherListState = watch(weatherListControllerProvider);
@override
Widget build(BuildContext context, ScopedReader watch) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField(
decoration: new InputDecoration(hintText: "Type in here"),
//onChanged is called whenever we add or delete something on Text Field
onChanged: (String str) {
result = str;
// watch(weatherRepositoryProvider).retrieveWeather(location: str);
}),
//displaying input text
new ElevatedButton(
onPressed: () {
context
.read(weatherListControllerProvider)
.requestWeather(location: result);
},
child: Text('Get user info'),
)
],
),
);
}
}
Weather_list_controller.dart which defines requestWeather function.
class WeatherListController extends StateNotifier<AsyncValue<Weather>> {
final Reader _read;
WeatherListController(this._read) : super(AsyncValue.loading()) {
requestWeather();
}
Future<void> requestWeather({bool isRefreshing = false, String? loc}) async {
if (isRefreshing) state = AsyncValue.loading();
if (loc == null) {
loc = 'Los Angeles';
}
try {
final weathers =
await _read(weatherRepositoryProvider).retrieveWeather(location: loc);
if (mounted) {
state = AsyncValue.data(weathers);
// print(state);
}
// return weathers;
} catch (e) {
throw Exception(
'Something went wrong in WeatherListController !! ' + e.toString());
}
}
}