gpt4 book ai didi

list - 指定要通过json添加到列表中的项目数

转载 作者:行者123 更新时间:2023-12-03 04:47:54 26 4
gpt4 key购买 nike

如何加载要通过json添加到列表中的特定数量的项目?例如,要通过json从20个列表中加载20个项目。

 Future<List<Mentor>> _getUsers() async {
sharedPreferences = await SharedPreferences.getInstance();
var data = await http.get(
NetworkUtils.host + AuthUtils.endPointMentors,
headers: {
'Authorization': "Bearer " + sharedPreferences.getString("token"),
'Accept': 'application/json'
},
);
var jsonData = json.decode(data.body);
for (var u in jsonData["data"]) {
Mentor user = Mentor(
u["id"]!=null ? u["id"]: "",
u["category"]!=null ? u["category"] : "",
u["email"]!=null ? u["email"]: "",
u["email_verified_at"] !=null ? u["email_verified_at"]: "",
u["first_name"] !=null ? u["first_name"]: "",
u["last_name"] != null ?u["last_name"]: "",
u["other_name"] !=null ? u["other_name"]: "",
u["country"] !=null ? u["country"]: "",
u["industry"] !=null ? u["industry"]: "",
u["gender"] !=null ? u["gender"]: "",
u["bio_interest"] !=null ? u["bio_interest"]: "",
u["phone"] !=null ?u["phone"]: "",
u["state_of_origin"] !=null ? u["state_of_origin"]: "",
u["fav_quote"] != null ? u["fav_quote"]: "",
u["profile_image"] != null ? u["profile_image"] : "",
u["terms"] !=null ? u["terms"]: "",
u["check_status"] !=null ? u["check_status"]: "",
u["current_job"] != null ? u["current_job"]: "",
u["created_at"] !=null ? u["created_at"]: "",
u["updated_at"]!=null ? u["updated_at"]: "",
u["social_id"] !=null ? u["social_id"]: "",
getFromList(u["employment"], 'company'),
getFromList(u['employment'], 'position'),
getFromList(u['education'], 'institution'),
getFromList(u['education'], 'degree'),);

users.add(user);
//this adds all the users to the list. I want say 10 items to be loaded into the list.
}
return filteredData;
}

因此,这会将所有数据加载到我的列表中。如何从列表中加载而不是全部加载10个项目?

最佳答案

您可以在take上使用 Iterable 方法返回新的Iterable,该Iterable仅返回前n个条目。因此,您应该能够像这样获得前10个条目:

...
var jsonData = json.decode(data.body);
for (var u in jsonData["data"].take(10)) {
Mentor user = Mentor(
...

关于list - 指定要通过json添加到列表中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61971058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com