Flutter 方法';请求天气';isn';t为类型';异步值';
我在试着理解《颤栗河荚》。我可以从服务中获取和显示数据,并了解使用AsyncValue的好处。但是,当我尝试在StateNotifier中调用requestWeather函数时,它应该依次调用retrieveWeather,并获取新位置的天气信息。但我得到了这个错误 没有为类型“AsyncValue”定义方法“requestWeather”。 请尝试将名称更正为现有方法的名称,或定义名为“requestWeather”的方法。dartundefined_method 在main.dart中,我试图使用提供程序访问requestWeather函数Flutter 方法';请求天气';isn';t为类型';异步值';,flutter,riverpod,Flutter,Riverpod,我在试着理解《颤栗河荚》。我可以从服务中获取和显示数据,并了解使用AsyncValue的好处。但是,当我尝试在StateNotifier中调用requestWeather函数时,它应该依次调用retrieveWeather,并获取新位置的天气信息。但我得到了这个错误 没有为类型“AsyncValue”定义方法“requestWeather”。 请尝试将名称更正为现有方法的名称,或定义名为“requestWeather”的方法。dartundefined_method 在main.dart中,我试
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'),
)
],
),
);
}
}
类InputLocationBox扩展了ConsumerWidget{
字符串结果=”;
//最终weatherListState=手表(weatherListControllerProvider);
@凌驾
小部件构建(BuildContext上下文,ScopeDrawer监视){
返回容器(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新文本字段(
装饰:新输入装饰(hintText:“在此处键入”),
//每当我们在文本字段中添加或删除某些内容时,都会调用onChanged
onChanged:(字符串str){
结果=str;
//手表(weatherRepositoryProvider)。检索天气(位置:str);
}),
//显示输入文本
新升降按钮(
已按下:(){
上下文
.read(weatherListControllerProvider)
.请求天气(位置:结果);
},
子项:文本(“获取用户信息”),
)
],
),
);
}
}
Weather\u list\u 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());
}
}
}
类WeatherListController扩展StateNotifier{
最终读者阅读;
WeatherListController(this.\u read):超级(AsyncValue.loading()){
请求天气();
}
Future requestWeather({bool isRefreshing=false,String?loc})异步{
如果(isRefreshing)状态=AsyncValue.loading();
如果(loc==null){
loc=‘洛杉矶’;
}
试一试{
最终天气=
等待读取(weatherRepositoryProvider)。检索天气(位置:loc);
如果(已安装){
状态=异步值。数据(天气);
//印刷品(国家);
}
//回归天气;
}捕获(e){
抛出异常(
“WeatherListController出现问题!!”+e.toString());
}
}
}
当使用StateNotifier并尝试访问这些方法时,它需要像这样的provider.notifier
context
.read(weatherListControllerProvider.notifier)
.requestWeather(loc: result);
我访问请求天气时遇到问题的小部件
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'),
)
],
),
);
}
}
类InputLocationBox扩展了ConsumerWidget{
字符串结果=”;
@凌驾
小部件构建(BuildContext上下文,ScopeDrawer监视){
最终weatherListState=watch(weatherListControllerProvider.notifier);
返回容器(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新文本字段(
装饰:新输入装饰(hintText:“在此处键入”),
//每当我们在文本字段中添加或删除某些内容时,都会调用onChanged
onChanged:(字符串str){
结果=str;
//手表(weatherRepositoryProvider)。检索天气(位置:str);
}),
//显示输入文本
新升降按钮(
已按下:(){
//上下文
//.read(weatherListControllerProvider)
//.请求天气(位置:结果);
打印(结果);
上下文
.read(weatherListControllerProvider.notifier)
.请求天气(loc:结果);
},
child:Text('Get Weather'),
)
],
),
);
}
}
这是否回答了您的问题?