【问题标题】:flutter foreach loop on json response from API来自API的json响应上的flutter foreach循环
【发布时间】:2021-07-28 11:32:32
【问题描述】:

我已在以下模型中转换了我的 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,
      };
}

我这样调用 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;
  }

现在我需要为此实现一个 for each 循环,以检查键“vehbodyopen”的值是“Y”还是“N”,并为它们创建单独的数组或对象,然后将它们显示在 ListViewBuilder 小部件中。

我是新来的颤振。我来自 javascript 背景。我通过在 json 响应中执行每个循环并将它们存储在两个不同的数组中来解决了 javascript 中的问题。如果可能的话,我在颤振和飞镖中寻找相同的解决方案。

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:
    for (int i = 0; i < vehicleModel.length; i++) {
        if(vehicleModel[i].vehbodyopen == "Y"){
        //do stuff
        }else{
        //do another stuff
      }
    }
    

    你可以试试这个

    【讨论】:

    • 您好,感谢您的建议,我尝试了您的解决方案,但无法获得车辆模型的长度。所以 for 循环没有执行。 vehicleModel 是 VehicleList 类型。
    • 感谢您的意见。通过对您的答案进行一些更改,我找到了解决方案。
    【解决方案2】:

    我找到了一个可能的解决方案,我必须遍历 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);
                  }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多