小编Art*_*rov的帖子

类型为“List<Data>?”的值 无法分配给“List<Data>”类型的变量

我正在尝试使用本教程从测试 API 中获取 Flutter 中的数据 - https://flutterforyou.com/how-to-fetch-data-from-api-and-show-in-flutter-listview/

当我复制代码时,VS Code 抛出此错误,我不明白,我需要做什么, 在此处输入图像描述

感谢您的回复,对于虚拟问题、代码示例提前表示歉意

    Future <List<Data>> fetchData() async {
  
  final response =
      await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums'));
  if (response.statusCode == 200) {
    List jsonResponse = json.decode(response.body);
      return jsonResponse.map((data) => Data.fromJson(data)).toList();
  } else {
    throw Exception('Unexpected error occured!');
  }
}

class Data {
  final int userId;
  final int id;
  final String title;

  Data({required this.userId, required this.id, required this.title});

  factory Data.fromJson(Map<String, dynamic> json) {
    return Data(
      userId: json['userId'],
      id: json['id'],
      title: json['title'],
    );
  }
}
class _MyAppState …
Run Code Online (Sandbox Code Playgroud)

async-await dart flutter

3
推荐指数
1
解决办法
2245
查看次数

标签 统计

async-await ×1

dart ×1

flutter ×1