Godwin_Joel
Godwin_Joel

Reputation: 11

flutter foreach loop on json response from API

I have converted my JSON response in the below model:

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:

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.

Im new to flutter. Im from javascript background. I solved the problem in javascript by executing for each loop in the json response and stored them in two different arrays. Im looking for the same solution in flutter and dart if possible.

Upvotes: 1

Views: 2533

Answers (2)

Godwin_Joel
Godwin_Joel

Reputation: 11

I found a possible solution I had to loop over the vehicles List inside the 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);
              }
            }

Upvotes: 0

lamaw01
lamaw01

Reputation: 21

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

you might try this

Upvotes: 2

Related Questions