'_TypeError (type 'String' is not a subtype of type 'Map<String, dynamic>') in API call flutter

Here is my model class and factory method,

class City {
  String? city;

  City({
    this.city,
  });

  factory City.fromJson(Map<String, dynamic> parsedJson) {
    return City(
      city: parsedJson["cities"],
    );
  }
}

here is the API call,
Exception occurs in the for loop. Exception was type 'String' is not a subtype of type 'Map<String, dynamic>'

Future<CityListState> getCityList() async {
    List<City> cityList = [];
    CityListState cityListState = const CityListSuccessfullyLoaded();
    ApiConnectionRepository(
      success: (title, message, data) {
        printLogs('cityy $data');
        final cities = (data["cities"] ?? []) as List;

         for(var element in cities ){
           print(element);
          cityList.add(City.fromJson(element));
         }

        cityListState = CityListSuccessfullyLoaded(city: cityList);
      },
      failed: (title, message, data) {
        cityListState = CityListLoadedFailed(title: title, message: message);
       
      },
    ).fetchProcessedData(ApiConstants.getCityList, {});

    return cityListState;
  }


Sources

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

Source: Stack Overflow

Solution Source