可遍历json数据波动

问题描述 投票:0回答:2

我有一个问题。我已经通过具有出色效果的api提出了get请求。我有一个从前一个屏幕收到的名为“ mentee id”的list。看起来像这样:

(2,4,121);

我需要此列表,因为在我的端点中,我需要将这些值中的每一个都以我的端点作为ID进行串联。

这是我的终点:

{{url}}/dashboard/mentee/{{mentee_id}}

因此,我需要遍历列表以接收每个受训者ID的数据,并将它们传递给listView。

这是我尝试实现这一目标的方法,但是我没办法了。

Mentee

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": "[email protected]",
    "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
}
json flutter dart iterable
2个回答
0
投票
  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'
          },
        )),
  );

0
投票

我认为您需要遍历请求而不是数据,因为您得到的响应只是单个对象,而不是列表。

一个例子是:

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);

   }
}

我希望我完全理解您想要实现的目标,并且这个答案对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.