Home > Software design >  Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype

Time:01-14

I am having a response below

[
    {
        "name": "Kamchatka",
        "about": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia,molestiae quas vel sint commodi repudiandae consequuntur voluptatum laborumnumquam blanditiis harum quisquam eius sed odit fugiat iusto fuga praesentium optio, eaque rerum! Provident similique accusantium nemo autem.",
        "email": "[email protected]",
        "index": 1,
        "picture": "https://thispersondoesnotexist.com/"
    },
    {
        "name": "Valjakutse",
        "about": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia,molestiae quas vel sint commodi repudiandae consequuntur voluptatum laborumnumquam blanditiis harum quisquam eius sed odit fugiat iusto fuga praesentium optio, eaque rerum! Provident similique accusantium nemo autem.",
        "email": "[email protected]",
        "index": 2,
        "picture": "https://thispersondoesnotexist.com/"
    },
    {
        "name": "Shipment",
        "about": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia,molestiae quas vel sint commodi repudiandae consequuntur voluptatum laborumnumquam blanditiis harum quisquam eius sed odit fugiat iusto fuga praesentium optio, eaque rerum! Provident similique accusantium nemo autem.",
        "email": "[email protected]",
        "index": 3,
        "picture": "https://thispersondoesnotexist.com/"
    }
]

I am having a Future which is async and should take the response and convert it into a List of HashMap like below


  Future<List<HashMap<String, dynamic>>> _fetchUsersListHashMap() async {
    try {
      final response = await http.get(Uri.parse("https://jsonkeeper.com/b/XBCA"));

      late List<HashMap<String, dynamic>> responseList;

      responseList = List<HashMap<String, dynamic>>.from(json.decode(response.body));
    
      String stringList = jsonEncode(responseList);

      log("FetchUsersListHashMap $stringList");

      return responseList;

    } catch (e) {

      log("FetchUsersListHashMapException $e");

      rethrow;
    }
  }

When i call _fetchUsersListHashMap i am getting below error


Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'HashMap<String, dynamic>'

I have even tried changing the way i am assigning responseList to

responseList = json.decode(response.body);

But its not assigning the list of HashMap with the response from the url

Below is how i did it in java its just that i cant be able to replicate the same concept in flutter

  List<HashMap<String, Object>> responseList = new Gson().fromJson(response,
                                new TypeToken<List<HashMap<String, Object>>>() {
                                }.getType());

CodePudding user response:

Future<List<HashMap<String, dynamic>>> _fetchUsersListHashMap() async {
 try {
  final response = await http.get(Uri.parse("https://jsonkeeper.com/b/XBCA"));

  final decoded = json.decode(response.body);

  final responseList = decoded
    .map<HashMap<String, dynamic>>((e) => HashMap<String, dynamic>.from(e))
    .toList();

  String stringList = jsonEncode(responseList);

  log("FetchUsersListHashMap $stringList");

  return responseList;
} catch (e) {
  log("FetchUsersListHashMapException $e");

  rethrow;
 }
}

CodePudding user response:

Try this:

  List<HashMap<String, dynamic>> responseList = [];
  List jsonList = List.from(jsonDecode(response.body));
  jsonList.forEach((element) {
    responseList.add(HashMap.from(Map.from(element)));
  });
  •  Tags:  
  • Related