小编Too*_*Man的帖子

无法使用 Flutter 项目将 JSON 映射到 Dart 中的列表

我运行的是 Flutter 1.0.0(第一个版本),最近升级到 1.2.1,有很多错误和警告需要纠正。主要指定注释类型。纠正所有这些问题后,我运行了 Android 应用程序,现在将 JSON 映射到列表的代码无法正常工作。首先,我将发布有效的原始代码。JSON 数据来自 HTTP 请求。

api.dart

Future<List<Device>> getDevices() async {
  var response = await _httpNetwork.get(_devicesUrl, headers: {"Cookie": _sessionId});

  if (response.statusCode < 200 || response.statusCode > 400) {
    throw Exception("Error while fetching data");
  }

  final body = json.decode(response.body);

  return body.map<Device>((device) => Device.fromJson(device)).toList();
}
Run Code Online (Sandbox Code Playgroud)

设备.dart

class Device {
  Device({
    this.id,
    this.name,
    this.uniqueId,
    this.status,
    this.phone,
    this.model,
    this.disabled,
  });

  factory Device.fromJson(Map<String, dynamic> json) => Device(
        id: json['id'],
        name: json['name'],
        uniqueId: json['uniqueId'],
        status: json['status'] == 'online' ? true : …
Run Code Online (Sandbox Code Playgroud)

json dart flutter

5
推荐指数
1
解决办法
2459
查看次数

标签 统计

dart ×1

flutter ×1

json ×1