【问题标题】:Iterable over json data flutter可迭代 json 数据颤动
【发布时间】: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


【解决方案1】:

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'
          },
        )),
  );

【讨论】:

    【解决方案2】:

    我认为你需要循环请求而不是数据,因为你得到的响应只是一个对象,而不是一个列表。

    一个例子是:

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

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

    【讨论】:

    • 这看起来很棒,但是我收到了这个错误。类型 'MappedListIterable' 不是类型 'List' 的子类型
    • 这是由于menteeIds的表示方式,您应该从中获取id。
    猜你喜欢
    • 2016-01-10
    • 2021-10-13
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多