方法 'requestWeather' 没有为类型 'AsyncValue' 定义
The method 'requestWeather' isn't defined for the type 'AsyncValue'
我正在尝试了解 Flutter Riverpod。我可以从服务中获取和显示数据,并了解使用 AsyncValue 的好处。但是,当我尝试在应该调用 retrieveWeather 依次获取新位置的天气信息。但是我得到这个错误。
方法 'requestWeather' 没有为类型 'AsyncValue' 定义。
尝试将名称更正为现有方法的名称,或定义名为 'requestWeather'.dartundefined_method
的方法
从 main.dart 我尝试使用 Provider 访问 requestWeather 函数。
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 定义了 requestWeather 函数。
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());
}
}
}
当使用 StateNotifier 并尝试访问这些方法时,它需要 provider.notifier 像这样
context
.read(weatherListControllerProvider.notifier)
.requestWeather(loc: result);
我无法访问 requestWeather
的小部件
class InputLocationBox extends ConsumerWidget {
String result = "";
@override
Widget build(BuildContext context, ScopedReader watch) {
final weatherListState = watch(weatherListControllerProvider.notifier);
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);
print(result);
context
.read(weatherListControllerProvider.notifier)
.requestWeather(loc: result);
},
child: Text('Get Weather'),
)
],
),
);
}
}
我正在尝试了解 Flutter Riverpod。我可以从服务中获取和显示数据,并了解使用 AsyncValue 的好处。但是,当我尝试在应该调用 retrieveWeather 依次获取新位置的天气信息。但是我得到这个错误。
方法 'requestWeather' 没有为类型 'AsyncValue' 定义。 尝试将名称更正为现有方法的名称,或定义名为 'requestWeather'.dartundefined_method
的方法从 main.dart 我尝试使用 Provider 访问 requestWeather 函数。
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 定义了 requestWeather 函数。
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());
}
}
}
当使用 StateNotifier 并尝试访问这些方法时,它需要 provider.notifier 像这样
context
.read(weatherListControllerProvider.notifier)
.requestWeather(loc: result);
我无法访问 requestWeather
的小部件class InputLocationBox extends ConsumerWidget {
String result = "";
@override
Widget build(BuildContext context, ScopedReader watch) {
final weatherListState = watch(weatherListControllerProvider.notifier);
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);
print(result);
context
.read(weatherListControllerProvider.notifier)
.requestWeather(loc: result);
},
child: Text('Get Weather'),
)
],
),
);
}
}