Search code examples
flutterhttpresponse

flutter error Expected a value of type 'Map<dynamic, dynamic>', but got one of type 'List<dynamic>'


When I Map response body, it gives error.

Expected a value of type 'Map<dynamic, dynamic>', but got one of type 'List'

I want to print(data["id"]);

 var response = await http.post(url, body: mpost);
 // messageBox(context, response.body, response.body, "ok");
 Map data = jsonDecode(response.body);
 print(data["id"]);

Solution

  • Your response.body is returned as List.

    To get the id, you can use for-loop.

    for(var i in jsonDecode(response.body)){
         print(i["id"]);
    }