抖动问题中的HTTP调用类型强制转换错误 [英] HTTP Call type cast error in flutter issue

查看:56
本文介绍了抖动问题中的HTTP调用类型强制转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过

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

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

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

But I get this analyzer error.

不能将类型为"Future"的值分配给类型为"Features"的变量. 尝试更改变量的类型,或将右侧的类型强制转换为功能".dart(invalid_assignment)

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)


编辑结束


EDIT END

我有这个http调用,功能"只是一个具有8个值的对象.

I have this http call, Features is just an object with 8 values.

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

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

Im getting this error and Im not sure how to fix it. It looks correct code wise. We normally do these on button clicks and pass the object around the application but we need this to work on init.

类型'Future'不是类型转换类型'特征'的子类型

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

推荐答案

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

You are trying to assign a Future<T> to a T. Normally you'd await the future, or you can use .then.

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

这篇关于抖动问题中的HTTP调用类型强制转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆