I use package dio in my flutter app. A get response from my api question. response.data get type _InternalLinkedHashMap<String, dynamic>. I need convert this value to Map<String, dynamic>. I tried many options but this does not work.

I have no way to change the server response. Any advice?


Solution 1: diegoveloper

Try this:

Map<String, dynamic>.from(yourData)


Solution 2: jamesdlin

You don't need to do any conversion between _InternalLinkedHashMap<K, V> and Map<K, V>: the former is already a subtype of the latter.

void main() async {
  final map = <String, int>{};
  print(map.runtimeType);
  print('${map is Map<String, int>}');
}

prints:

_InternalLinkedHashMap<String, int>
true

(Map's default constructor is a factory constructor that constructs a LinkedHashMap. LinkedHashMap's default constructor is also a factory constructor, and the implementation for the Dart VM constructs and returns an internal _InternalLinkedHashMap object.)


Note that this is true only when _InternalLinkedHashMap<K, V> uses the same K and V as Map. If they are parameterized on different types then you will need to perform an explicit conversion, but that's not the situation asked in this question.


Solution 3: Abdul Rahman Shamair

How I do it, is I converts the _InternalLinkedHashMap<dynamic, dynamic> to a HashMap using

var map = HashMap.from(value) // value is _InternalLinkedHashMap <dynamic, dynamic>

From here I can get the Class Method to convert the map to my object so that I can use it in the code, like this

User.fromJson(map) //  this return User object 

You can generate Dart Model classes directly from the JSON using this website.

Here is the code snippet of User.Json method:

SeatBookingModel.fromJson(Map<String, dynamic> json) {
userName= json['userName'];
userEmail= json['userEmail'];
 }

Happy Coding


Solution 4: Brett Sutton

If you have type safety fully enabled then you need need to cast the argument; after which you don't need to type the Map.from.

Map.from(yourData  as Map<String, dynamic>)


Solution 5: Fredrik_Borgstrom

Try this instead if your object is deeper than one level:

final Map<String, dynamic> convertedData = jsonDecode(jsonEncode(yourData));