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

标签 flutter dart

编辑

我试过了

@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/

相关文章:

firebase - Firestore 与其他用户共享数据

ios - 播放下一首歌曲时播放器中的flutter audio_manager ios延迟

Dart build runner 生成一个包含内容的 dart 文件

带有多部分/表单数据的 DART 帖子

json - 保存和加载数据有哪些可能性?

flutter - 屏幕一侧的绿色 strip 有 video_player 抖动

Flutter:在包中指定和使用 Assets

facebook-graph-api - 如何使用 flutter 从 facebook graph api 获取更好的个人资料图片

dart - list.elementAt(n) 和 list[n] 之间有什么区别吗

android - Flutter:默认选项卡栏 Controller 在滑动后不保持状态