Flutter 类型为can';无法从方法返回,因为它的返回类型为 List getData()异步{ var response=wait http.get(url); 如果(response.statusCode==200){ 字符串数据=response.body; 打印(数据); var温度=jsonDecode(数据)['main']['temp']; var condition=jsonDecode(数据)['weather'][0]['id']; var city_name=jsonDecode(数据)['name']; 返回[温度、条件、城市名称]; }否则{ 打印(响应状态码); } } }

Flutter 类型为can';无法从方法返回,因为它的返回类型为 List getData()异步{ var response=wait http.get(url); 如果(response.statusCode==200){ 字符串数据=response.body; 打印(数据); var温度=jsonDecode(数据)['main']['temp']; var condition=jsonDecode(数据)['weather'][0]['id']; var city_name=jsonDecode(数据)['name']; 返回[温度、条件、城市名称]; }否则{ 打印(响应状态码); } } },flutter,dart,Flutter,Dart,我收到一个奇怪的错误,说我不能返回列表,因为我希望返回列表。由于函数get data是异步的,它应该返回Future示例如下: List<String> getData() async { var response = await http.get(url); if (response.statusCode == 200) { String data = response.body; print(data); var

我收到一个奇怪的错误,说我不能返回
列表
,因为我希望返回
列表

由于函数get data是异步的,它应该返回
Future
示例如下:

List<String> getData() async {
    var response = await http.get(url);

    if (response.statusCode == 200) {
        String data = response.body;
        print(data);
        var temperature = jsonDecode(data)['main']['temp'];
        var condition = jsonDecode(data)['weather'][0]['id'];
        var city_name = jsonDecode(data)['name'];

        return [temperature, condition, city_name];
        } else {
             print(response.statusCode);
        }
   }
}
Future<List<String>> getData() async {
    var response = await http.get(url);

    if (response.statusCode == 200) {
        String data = response.body;
        print(data);
        var temperature = jsonDecode(data)['main']['temp'];
        var condition = jsonDecode(data)['weather'][0]['id'];
        var city_name = jsonDecode(data)['name'];

        return <String>[temperature, condition, city_name];
    } else {
             print(response.statusCode);
    }

}

一般来说,在异步函数中出现此错误时:

无法从方法返回类型为x的值,因为它具有 x的返回类型

这条消息看起来很奇怪,但它可能意味着您缺少了未来的返回类型

因此,将Future添加到您的方法返回类型中:

例如:

String data = response.body;
var decodedData = jsonDecode(data);
var temperature = decodedData['main']['temp'];
var condition = decodedData['weather'][0]['id'];
var city_name = decodedData['name'];
List getValues()异步
{
List List=等待getJson();
退货清单;
}
改为:

  List<String> getValues() async
  {
    List<String> list = await getJson();
    return list;
  }
Future getValues()异步
{
List List=等待getJson();
退货清单;
}
  Future<List<String>> getValues() async
  {
    List<String> list = await getJson();
    return list;
  }