【发布时间】:2021-09-30 05:42:15
【问题描述】:
我想从上面的 JSON 响应中返回 name 的前五个值
Future <List<UserModel>> fetchData() async {
final response =
await http.get(Uri.parse(URLCONST.API_URL));
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((user) => new UserModel.fromJson(user)).toList();
} else {
throw Exception('Unexpected error occured!');
}
}
这是下面的模型。
class UserModel {
int id;
String name;
UserModel({
required this.id,
required this.name,
});
factory UserModel.fromJson(Map<String, dynamic> json) {
return UserModel(
id: json['id'],
name: json['name'],
);
}
}
API 在列表中有几个值,我想只返回前 5 个
【问题讨论】: