【发布时间】:2020-06-07 11:48:33
【问题描述】:
伙计们,我有一个问题。我已经通过一个带有颤振的 api 提出了get 请求,它运行良好。
我有一个名为“mentee id”的list,这是我从之前的屏幕收到的。它看起来像这样:
(2,4,121);
我需要这个列表,因为在我的端点中,我需要将这些值中的每一个与我的端点作为 id 连接起来。
这是我的终点:
{{url}}/dashboard/mentee/{{mentee_id}}
所以我需要遍历我的列表以接收每个学员 ID 的数据并将它们传递给 listView。
这是我尝试实现这一目标的方式,但我没有想法。
学员
class Mentee {
var category;
var email;
var email_verified_at;
var first_name;
var last_name;
var other_name;
var country;
var industry;
var gender;
var bio_interest;
var phone;
var state_of_origin;
var fav_quote;
var profile_image;
var terms;
bool isAdmin = false;
var check_status = 0;
var current_job ;
var created_at;
var updated_at;
var social_id = 0;
var id = 0;
Mentee(this.category, this.email, this.email_verified_at, this.first_name, this.last_name, this.other_name,
this.country, this.industry, this.gender, this.bio_interest, this.phone, this.state_of_origin, this.fav_quote, this.profile_image, this.terms, this.isAdmin,
this.check_status, this.current_job, this.created_at, this.updated_at, this.social_id, this.id);
}
仪表板
Future _getIndex() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var data = await http.get(
//could not figure out how to get this index
NetworkUtils.host + AuthUtils.endPointMenteeProfile+ index,
headers: {
'Authorization': "Bearer " + sharedPreferences.getString("token"),
'Accept': 'application/json'
},
);
var jsonData = json.decode(data.body);
for (var index = 0; index < widget.menteeIds.length; ++index) { {
Mentee user = Mentee(
u["category"],
u["email"],
u["email_verified_at"],
u["first_name"],
u["last_name"],
u["other_name"],
u["country"],
u["industry"],
u["gender"],
u["bio_interest"],
u["phone"],
u["state_of_origin"],
u["fav_quote"],
u["profile_image"],
u["terms"],
u["isAdmin"],
u["check_status"],
u["current_job"],
u["created_at"],
u["updated_at"],
u["social_id"],
u["id"]);
users.add(user);
}
print(users.length.toString());
}
回应
{
"id": 2,
"category": "mentee",
"email": "tochukwuodoeme@yahoo.com",
"email_verified_at": null,
"first_name": "Gift",
"last_name": "Hazard",
"other_name": null,
"country": "Afganistan",
"industry": null,
"gender": null,
"bio_interest": "Positive vibes",
"phone": "7051204606",
"state_of_origin": null,
"fav_quote": "Hope it works",
"profile_image": "2_profile_image1559953374.jpg",
"terms": null,
"isAdmin": "0",
"check_status": null,
"current_job": null,
"created_at": "2019-05-23 13:25:30",
"updated_at": "2020-01-23 00:46:32",
"social_id": null
}
【问题讨论】:
-
您为什么要遍历
widget.menteeIds并将它们分配给用户?我有点迷路了,我可以得到一个上下文吗?widget.menteeIds是您在问题开头提到的列表吗?响应是什么 `NetworkUtils.host + AuthUtils.endPointMenteeProfile+ index,`,它是什么样的?抱歉,需要更多信息才能给出决定性的答案。 -
刚刚更新了我的问题。请查看
-
我需要遍历列表,将每个响应保存在 Mentee 对象中并将它们添加到列表视图中。你现在明白了吗?
-
u来自哪里。它不会更改提供的代码。你不应该使用jsonData而不是u吗?
标签: json flutter dart iterable