1

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.

2 Answers 2

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

you might try this

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for the suggestion, i tried ur solution but i couldn't get the length of the vehicleModel. so the for loop is not executing. The vehicleModel is of type VehicleList.
Thanks for the input. I found a solution by making some changes to your answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.