【发布时间】:2020-01-24 23:50:26
【问题描述】:
我的 API 返回以下响应:
[{id: 1, nome: foo}, {id: 2, nome: bar}]
我创建了一个模型Client 来代表每个人:
class Client {
final int id;
final String name;
Client({
this.id,
this.name,
});
factory Client.fromJson(Map<String, dynamic> json) {
return Client(
id: json['id'],
name: json['nome'],
);
}
Map<String, dynamic> toJson() => {
'id': id,
'nome': name,
};
}
那么,在我的仓库中,上面获取数据的方法如下:
Future<List<Client>> getClients() async {
try {
final _response = await _dio.get(
'/clientes',
options: Options(
headers: {'Authorization': 'Bearer $TOKEN'},
),
);
return Client.fromJson(_response.data[0]) as List; // Error pointed to this line
} on DioError catch (_e) {
throw _e;
}
}
存储在这里
@observable
List<Client> clients;
我不知道该怎么做。我做错了什么?
【问题讨论】: