gpt4 book ai didi

flutter - 抖动问题中的HTTP调用类型强制转换错误

转载 作者:行者123 更新时间:2023-12-03 04:59:58 26 4
gpt4 key购买 nike

编辑

我试过了

@override
void initState () {
super.initState();

features = getFeatureStatus(user.userId).then((f) {
setState((){features = f;});
});
}

但是我得到这个分析器错误。

A value of type 'Future' can't be assigned to a variable of type 'Features'. Try changing the type of the variable, or casting the right-hand type to 'Features'.dart(invalid_assignment)



编辑结束

我有这个http调用,“功能”只是一个具有8个值的对象。

HTTP call
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;
}

我收到此错误,我不确定如何解决。它看起来是正确的代码。通常,我们在单击按钮时执行这些操作,然后将对象传递给应用程序,但是我们需要在初始化时进行操作。

type 'Future' is not a subtype of type 'Features' in type cast

最佳答案

您正在尝试将Future<T>分配给T。通常,您将来会使用await,也可以使用.then

  getFeatureStatus(widget.user.userId).then((f){
setState((){features = f;});
});

关于flutter - 抖动问题中的HTTP调用类型强制转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59723496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com