我试图从 Future 对象中获取值,但 vsc 总是说
The getter 'wetter' isn't defined for the type 'Object'.
Try importing the library that defines 'wetter', correcting the name to the name of an existing getter, or defining a getter or field named 'wetter'.dartundefined_getter
那是我的代码,错误出现在
<块引用>grad = snapshot.data!.wetter
void refreshWeather() {
final Future<Wetter> wetter = Wetterapi.fetchWetter();
FutureBuilder(
future: wetter,
builder: (context, snapshot) {
if (snapshot.hasData) {
grad = snapshot.data!.wetter;
}
throw '';
});
}
更湿的类看起来像这样
class Wetter {
final String wetter;
final double temperatur;
final String wind;
Wetter({
required this.wetter,
required this.temperatur,
required this.wind,
});
factory Wetter.fromJson(Map<String, dynamic> json) {
return Wetter(
wetter: json['weather.main'],
temperatur: json['main.temp'],
wind: json['wind.speed'],
);
}
String get gewetter {
return wetter;
}
}
我错过了什么吗?
答案 0 :(得分:0)
定义FutureBuilder
的类型参数:
// vvvvvvvv
FutureBuilder<Wetter>(
future: wetter,
builder: (context, snapshot) {
if (snapshot.hasData) {
grad = snapshot.data!.wetter;
}
throw '';
},
);