【发布时间】:2020-01-14 07:53:14
【问题描述】:
我是 Flutter 的新手,所以我不确定我应该如何进行 API 调用,来自我使用 Retrofit 的 Java。
JSON 响应:
{
"total": 13,
"rows": [
{
"id": 1,
"name": "Name"
}
]
}
模型类:
class Category {
int total;
List<Rows> rows;
Category({this.total, this.rows});
Category.fromJson(Map<String, dynamic> json) {
total = json['total'];
if (json['rows'] != null) {
rows = new List<Rows>();
json['rows'].forEach((v) {
rows.add(new Rows.fromJson(v));
});
}
}
}
class Rows {
String name;
Rows({this.name});
Rows.fromJson(Map<String, dynamic> json) {
name = json['name'];
}
}
主类
List<Rows> rows = [];
Future<List<Rows>> getData() async {
var response = await http.get(
Uri.encodeFull("http://192.168.0.10/api/v1/categories"),
headers: {
"Authorization": "API",
"Accept": "application/json"
}
);
var jsonData = json.decode(response.body);
}
我不知道如何处理我尝试使用 Rows.fromJson() 获取对象,但我只得到'Instance of Rows',并且通过调用 name 我得到 null。
【问题讨论】: