我运行的是 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)