Home > Blockchain >  Flutter display nested json in ListView return type String is not a subtype of type 'Map<Str
Flutter display nested json in ListView return type String is not a subtype of type 'Map<Str

Time:07-03

JSON RESPONSE { "data": { "Address": "xyz", "CityID": "1", "CityName": "xyz", "CompanyName": "xyz", "CreditDays": "12", "CusotmerID": "45", "Email": "[email protected]", "GSTNo": "1234", "IsApproved": "False", "Lat": "", "Long": "", "Mobile": "1234567891", "Pincode": "", "Route": "", "StateID": "1", "StateName": "xyz", "UniqueNumber": "" }, "message": "Data updated successfully", "status": 200 }

->>>> API SERVICES List Customerlist = [];

Future<List<GetCustomer>> getPostApi() async {
final response = await http.post(
    Uri.parse(
        'xyz'),
    headers: {
      "Content-Type": "application/json",
      "Charset": "utf-8",
    },
    body: (jsonEncode({'CustomerID': 1})));

if (response.statusCode == 200) {
  // Map<String, dynamic> map = json.decode(response.body);

  Map<String, dynamic> map =
      new Map<String, dynamic>.from(json.decode(response.body));

  List<dynamic> data = map["data"];

  Customerlist.clear();
  for (var i in data) {
    Customerlist.add(GetCustomer.fromJson(i));
  }
  return Customerlist;
} else {
  return Customerlist;
}

} -----> ERROR WHEN CALLING THIS SERVICES ERROR('String is not a subtype of type 'Map<String, dynamic>');

CodePudding user response:

You have data in response is Object type not an Array. No need for loop

 Map<String, dynamic> map =
      new Map<String, dynamic>.from(json.decode(response.body));

  var data = map["data"];

Customerlist.clear();
Customerlist.add(GetCustomer.fromJson(data));
  • Related