【问题标题】:HTTP Call type cast error in flutter issue颤动问题中的HTTP调用类型转换错误
【发布时间】: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... - 查看答案

标签: flutter dart


【解决方案1】:

您正在尝试将Future&lt;T&gt; 分配给T。通常你会在未来使用await,或者你可以使用.then

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

【讨论】:

  • 完全重现上面的代码。不要在开头添加无关的features=
  • 请注意,在收到 http 响应之前,功能将为空,因此在您的构建中允许这样做 - 例如,在进度指示器为空时呈现进度指示器。
  • 这行得通,它抛出了一个快速错误,几乎立即消失,我检查其中一个值及其空值,然后它可以正常工作。有什么办法可以防止这种情况发生吗?
  • 见上一行的评论。
【解决方案2】:
import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // this is randomly declared
  Features features;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getFeatureStatus();
    // Or you can use the second method
    getFeatureStatus(user.userId).then((f) {
      // here you will get the features object where you can later play with it
    });
  }

  getFeatureStatus() async {
    features = await getFeatureStatus(user.userId);
    // in this you can later us the objct in the way you want
  }

  Future<Features> getFeatureStatus(String userID) async {
    Features _features;

    final response = await http.post('http://35.226.136.162/api/FeaturesGet',
        headers: {
          "Content-Type": "application/json",
          'Accept': 'application/json',
        },
        body: json.encode({'userID': userID}));
    _features = Features.fromJson(json.decode(response.body));

    return _features;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(child: Center(child: Text('Loading Data'))),
      ),
    );
  }
}

class Features {
  // your defination defined 
}

【讨论】:

    猜你喜欢
    • 2020-12-29
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2021-12-10
    • 1970-01-01
    • 2011-11-17
    相关资源
    最近更新 更多