'type 'List<dynamic>' is not a subtype of type 'List<Parking>'

I'm calling an API but I've been trying to parse this JSON response correctly but I haven't been able to resolve this issue. I call the API, and want to display the data but I get that error Type type 'List' is not a subtype of type 'List' or return 'null'. I'm not sure what am I doing wrong. The code is below:

class Parking {
  String name;
  String adress;
  String description;
  int totalParkingLots;
  int totalFreeParkingLots;
  int latitude;
  int longitude;
  int parkingFees;
  String companyId;
  String status;

  Parking(
      {
      this.name,
      this.adress,
      this.description,
      this.totalParkingLots,
      this.totalFreeParkingLots,
      this.latitude,
      this.longitude,
      this.parkingFees,
      this.companyId,
      this.status,
    });

  Parking.fromJson(Map<String, dynamic> json) {
    name = json['Name'];
    adress = json['Adress'];
    description = json['description'];
    totalParkingLots = json['totalParkingLots'];
    totalFreeParkingLots = json['totalFreeParkingLots'];
    latitude = json['latitude'];
    longitude = json['longitude'];
    parkingFees = json['ParkingFees'];
    companyId = json['companyId'];
    status = json['Status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Name'] = this.name;
    data['Adress'] = this.adress;
    data['description'] = this.description;
    data['totalParkingLots'] = this.totalParkingLots;
    data['totalFreeParkingLots'] = this.totalFreeParkingLots;
    data['latitude'] = this.latitude;
    data['longitude'] = this.longitude;
    data['ParkingFees'] = this.parkingFees;
    data['companyId'] = this.companyId;
    data['Status'] = this.status;
    return data;
  }
}
  static Future<List<Parking>> getparkings() async {
    List<Parking> _parkings = [];
    try {
      final uri = Uri.parse(ApiManager.parkingsAPIURL);
      final headers = {'Content-Type': 'application/json'};
      var response = await http.Client().get(
        uri,
        headers: headers,
      );

      int statusCode = response.statusCode;
      print(statusCode);
      var jsonResp = jsonDecode(response.body);

       if (jsonResp != null) {
          _parkings = jsonResp.map((item) => Parking.fromJson(item)).toList();
        }
        return _parkings;
    } catch (e) {
       print(e);
    }
  }

I am almost certain that the issue with the API call and the response because I'm not parsing the JSON data correctly but maybe I'm wrong and something else is the issue...



Solution 1:[1]

They way that you are doing is correct, nothing is wrong with the JSON response, like @mmcdon20 mentioned, for some reason Flutter is unable to detect the correct return type in map() function. But i'm guessing there is something to do with jsonResp, because it is dynamic type, and dynamic don't really have a map() function.

But for now, to solve this problem you can just cast the return type in the map() function as such:

_parkings = jsonResp.map<Parking>((item) => Parking.fromJson(item)).toList();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sunn