【发布时间】:2021-06-20 15:34:46
【问题描述】:
我正在尝试将这些复杂的 JSON 数据解析到 Flutter 应用程序中。我能够成功获取 JSON 数据,但无法在应用程序中显示数据。请参阅下面的代码和 JSON 数据。 我做错了什么?
{
"response": {
"cars": [
[
{
"id": 1,
"name": "Ford Mustang GT",
"class": "Muscle Car"
},
{
"id": 2,
"name": "Dodge Challenger",
"class": "Muscle Car"
},
{
"id": 3,
"name": "Chevrolet Camaro",
"class": "Muscle Car"
},
{
"id": 4,
"name": "Pontiac Firebird",
"class": "Muscle Car"
}
]
]
}
}
class Cars {
Cars({
this.id,
this.name,
this.carClass,
});
int id;
String name;
String carClass;
factory Cars.fromJson(Map<String, dynamic> json) => Cars(
id: json["id"],
name: json["name"],
carClass: json["class"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"class": carClass,
};
}
API Service getting the JSON Data
class ApiService {
Future<List<Cars>> getCars() async {
http.Response response = await http.get('http://localhost:3000/response');
var body;
if (response.statusCode == 200) {
body = jsonDecode(response.body);
List<dynamic> carList = body['response'];
print(body);
List<Cars> res =
carList.map((dynamic item) => Cars.fromJson(item)).toList();
return res;
}
}
}
【问题讨论】:
-
运行代码会得到什么?
-
@HuthaifaMuayyad 运行代码时,屏幕上没有数据。圆形进度条一直在旋转。
标签: arrays json flutter parsing dart