简体   繁体   English

flutter foreach 循环上 json 响应来自 API

[英]flutter foreach loop on json response from API

I have converted my JSON response in the below model:我在下面的 model 中转换了我的 JSON 响应:

VehicleList vehicleListFromJson(String str) =>
    VehicleList.fromJson(json.decode(str));

String vehicleListToJson(VehicleList data) => json.encode(data.toJson());

class VehicleList {
  VehicleList({
    this.vehicles,
  });

  List<Vehicle> vehicles;

  factory VehicleList.fromJson(Map<String, dynamic> json) => VehicleList(
        vehicles: List<Vehicle>.from(
            json["vehicles"].map((x) => Vehicle.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "vehicles": List<dynamic>.from(vehicles.map((x) => x.toJson())),
      };
}

class Vehicle {
  Vehicle({
    this.vehid,
    this.vehname,
    this.vehdescrip,
    this.vehbodyopen,
    this.vehbodyopenimg,
    this.vehbodyclose,
    this.vehbodycloseimg,
  });

  String vehid;
  String vehname;
  String vehdescrip;
  String vehbodyopen;
  String vehbodyopenimg;
  String vehbodyclose;
  String vehbodycloseimg;

  factory Vehicle.fromJson(Map<String, dynamic> json) => Vehicle(
        vehid: json["vehid"],
        vehname: json["vehname"],
        vehdescrip: json["vehdescrip"],
        vehbodyopen: json["vehbodyopen"],
        vehbodyopenimg: json["vehbodyopenimg"],
        vehbodyclose: json["vehbodyclose"],
        vehbodycloseimg: json["vehbodycloseimg"],
      );

  Map<String, dynamic> toJson() => {
        "vehid": vehid,
        "vehname": vehname,
        "vehdescrip": vehdescrip,
        "vehbodyopen": vehbodyopen,
        "vehbodyopenimg": vehbodyopenimg,
        "vehbodyclose": vehbodyclose,
        "vehbodycloseimg": vehbodycloseimg,
      };
}

I make the API call like this:我这样调用 API :

Future<VehicleList> getVehicles() async {
    var client = http.Client();
    var vehicleModel = null;

    try {
      var response = await client.get(Uri.parse(Strings.getVehiclesUrl));
      if (response.statusCode == 200) {
        var jsonString = response.body;
        var jsonMap = json.decode(jsonString);
        vehicleModel = VehicleList.fromJson(jsonMap);
      }
    } catch (Exception) {
      return vehicleModel;
    }
    return vehicleModel;
  }

Now I need to implement a for each loop on this to check if the value for key "vehbodyopen" is "Y" or "N" and create seperate arrays or objects for them and then display them in a ListViewBuilder widget.现在我需要为此实现一个 for each 循环,以检查键“vehbodyopen”的值是“Y”还是“N”,并为它们创建单独的 arrays 或对象,然后在 ListViewBuilder 小部件中显示它们。

Im new to flutter.我是 flutter 的新手。 Im from javascript background.我来自 javascript 背景。 I solved the problem in javascript by executing for each loop in the json response and stored them in two different arrays.我通过执行 json 响应中的每个循环来解决 javascript 中的问题,并将它们存储在两个不同的 arrays 中。 Im looking for the same solution in flutter and dart if possible.如果可能的话,我在 flutter 和 dart 中寻找相同的解决方案。

for (int i = 0; i < vehicleModel.length; i++) {
    if(vehicleModel[i].vehbodyopen == "Y"){
    //do stuff
    }else{
    //do another stuff
  }
}

you might try this你可以试试这个

I found a possible solution I had to loop over the vehicles List inside the vehicleModel.我找到了一个可能的解决方案,我必须遍历 vehicleModel 中的车辆列表。

List vehList = vehicleModel.vehicles;
            List openVehList = [];
            List closedVehList = [];
    
            for (var veh in vehList) {
              print('executing foreach');
              if (veh.vehbodyopen == "Y") {
                openVehList.add(veh);
              } else {
                closedVehList.add(veh);
              }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM