Я хочу читать эти JSON данные из веб-сервиса динамически во флаттере, и я хочу получить данные из веб-сервиса и создать представление горизонтального списка с помощью Future Builder. Я получил ответ json, но не смог прочитать в соответствии с упомянутыми классами
{
"count": 4,
"result": [
{
"deviceId": "kfr",
"location": {
"iconId": 1,
"id": 3,
"name": "ram's room",
"timestamp": 1586927632,
"userId": 1
},
"name": "Kitchen Fridge",
"timestamp": 1587481358
},
{
"deviceId": "kf",
"location": {
"iconId": 2,
"id": 4,
"name": "ram's room",
"timestamp": 1586935457,
"userId": 1
},
"name": "Kitchen Fan",
"timestamp": 1587481484
},
{
"deviceId": "ks",
"location": {
"iconId": 3,
"id": 5,
"name": "ram's room",
"timestamp": 1586935457,
"userId": 1
},
"name": "Kitchen Speaker",
"timestamp": 1587481554
},
{
"deviceId": "kth",
"location": {
"iconId": 4,
"id": 6,
"name": "ram's room",
"timestamp": 1586935457,
"userId": 1
},
"name": "Kitchen Thermostat",
"timestamp": 1587481587
}
]
}
Я создал эти классы для использования и хочу прочитать данные Json динамически. Спасибо
class DeviceDetails {
int count;
List<DeviceResult> result;
DeviceDetails({this.count, this.result});
DeviceDetails.fromJson(Map<String, dynamic> json) {
count = json['count'];
if (json['result'] != null) {
result = new List<DeviceResult>();
json['result'].forEach((v) {
result.add(new DeviceResult.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['count'] = this.count;
if (this.result != null) {
data['result'] = this.result.map((v) => v.toJson()).toList();
}
return data;
}
}
class DeviceResult {
String deviceId;
DeviceLocation location;
String name;
int timestamp;
DeviceResult({this.deviceId, this.location, this.name, this.timestamp});
DeviceResult.fromJson(Map<String, dynamic> json) {
deviceId = json['deviceId'];
location = json['location'] != null
? new DeviceLocation.fromJson(json['location'])
: null;
name = json['name'];
timestamp = json['timestamp'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['deviceId'] = this.deviceId;
if (this.location != null) {
data['location'] = this.location.toJson();
}
data['name'] = this.name;
data['timestamp'] = this.timestamp;
return data;
}
}
class DeviceLocation {
int iconId;
int id;
String name;
int timestamp;
int userId;
DeviceLocation({this.iconId, this.id, this.name, this.timestamp, this.userId});
DeviceLocation.fromJson(Map<String, dynamic> json) {
iconId = json['iconId'];
id = json['id'];
name = json['name'];
timestamp = json['timestamp'];
userId = json['userId'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['iconId'] = this.iconId;
data['id'] = this.id;
data['name'] = this.name;
data['timestamp'] = this.timestamp;
data['userId'] = this.userId;
return data;
}
}