【发布时间】:2020-04-30 13:43:57
【问题描述】:
编辑
我试过了
@override
void initState () {
super.initState();
features = getFeatureStatus(user.userId).then((f) {
setState((){features = f;});
});
}
但是我得到了这个分析器错误。
“Future”类型的值不能分配给“Features”类型的变量。 尝试更改变量的类型,或将右侧类型转换为 'Features'.dart(invalid_assignment)
编辑结束
我有这个 http 调用,Features 只是一个有 8 个值的对象。
HTTP 调用
Future<Features> getFeatureStatus(String userID) async {
final response = await http.post(
'http://url/api/FeaturesGet',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: json.encode({'userID' : userID }),
);
// If the call to the server was successful, parse the JSON
return Features.fromJson(json.decode(response.body));
}
实施
@override
void initState () {
super.initState();
features = getFeatureStatus(user.userId) as Features;
}
我收到此错误,但我不确定如何修复它。它看起来是正确的代码。我们通常在按钮单击时执行这些操作并在应用程序周围传递对象,但我们需要在 init 上工作。
类型“未来”不是类型转换中“特征”类型的子类型
【问题讨论】:
-
你不能这样做:
features = getFeatureStatus( ... etc...- 查看答案