可迭代 json 数据波动
Iterable over json data flutter
伙计们,我有一个问题。我已经通过 flutter 对 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
}
map
每个请求的id。
var datas = await Future.wait(
widget.menteeIds.map((index) => http.get(
NetworkUtils.host + AuthUtils.endPointMenteeProfile + index,
headers: {
'Authorization': "Bearer " + sharedPreferences.getString("token"),
'Accept': 'application/json'
},
)),
);
我认为您需要遍历请求而不是数据,因为您得到的响应只是一个对象,而不是列表。
例如:
Future<dynamic> _getMenteeJsonByIndex(String index) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var data = await http.get(
NetworkUtils.host + AuthUtils.endPointMenteeProfile+ index,
headers: {
'Authorization': "Bearer " + sharedPreferences.getString("token"),
'Accept': 'application/json'
},
);
var jsonData = json.decode(data.body);
return jsonData;
}
Future<void> _updateListOfMentees(List<String> menteeIds) async {
for(int i = 0; i < menteeIds.length; i++){
final dynamic menteeJson = await _getMenteeJsonByIndex(menteeIds[i]);
Mentee user = Mentee(
menteeJson["category"],
menteeJson["email"],
menteeJson["email_verified_at"],
menteeJson["first_name"],
menteeJson["last_name"],
menteeJson["other_name"],
menteeJson["country"],
menteeJson["industry"],
menteeJson["gender"],
menteeJson["bio_interest"],
menteeJson["phone"],
menteeJson["state_of_origin"],
menteeJson["fav_quote"],
menteeJson["profile_image"],
menteeJson["terms"],
menteeJson["isAdmin"],
menteeJson["check_status"],
menteeJson["current_job"],
menteeJson["created_at"],
menteeJson["updated_at"],
menteeJson["social_id"],
menteeJson["id"]);
users.add(user);
}
}
我希望我完全理解你想要实现的目标,希望这个答案对你有所帮助。
伙计们,我有一个问题。我已经通过 flutter 对 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
}
map
每个请求的id。
var datas = await Future.wait(
widget.menteeIds.map((index) => http.get(
NetworkUtils.host + AuthUtils.endPointMenteeProfile + index,
headers: {
'Authorization': "Bearer " + sharedPreferences.getString("token"),
'Accept': 'application/json'
},
)),
);
我认为您需要遍历请求而不是数据,因为您得到的响应只是一个对象,而不是列表。
例如:
Future<dynamic> _getMenteeJsonByIndex(String index) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var data = await http.get(
NetworkUtils.host + AuthUtils.endPointMenteeProfile+ index,
headers: {
'Authorization': "Bearer " + sharedPreferences.getString("token"),
'Accept': 'application/json'
},
);
var jsonData = json.decode(data.body);
return jsonData;
}
Future<void> _updateListOfMentees(List<String> menteeIds) async {
for(int i = 0; i < menteeIds.length; i++){
final dynamic menteeJson = await _getMenteeJsonByIndex(menteeIds[i]);
Mentee user = Mentee(
menteeJson["category"],
menteeJson["email"],
menteeJson["email_verified_at"],
menteeJson["first_name"],
menteeJson["last_name"],
menteeJson["other_name"],
menteeJson["country"],
menteeJson["industry"],
menteeJson["gender"],
menteeJson["bio_interest"],
menteeJson["phone"],
menteeJson["state_of_origin"],
menteeJson["fav_quote"],
menteeJson["profile_image"],
menteeJson["terms"],
menteeJson["isAdmin"],
menteeJson["check_status"],
menteeJson["current_job"],
menteeJson["created_at"],
menteeJson["updated_at"],
menteeJson["social_id"],
menteeJson["id"]);
users.add(user);
}
}
我希望我完全理解你想要实现的目标,希望这个答案对你有所帮助。