Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - how _InternalLinkedHashMap<String, dynamic> convert to Map<String, dynamic>?

Tags:

json

flutter

dart

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?

like image 278
FetFrumos Avatar asked Sep 08 '19 20:09

FetFrumos


People also ask

How do I convert dynamic to dart map?

Using Iterable forEach() method We can convert Dart List of Objects to Map in another way: forEach() method. var map = {}; list. forEach((customer) => map[customer.name] = {'email': customer.

What is _InternalLinkedHashMap?

_InternalLinkedHashMap<dynamic, dynamic> can take any type for a key. Map<String, dynamic> can take only String s as keys. Therefore _InternalLinkedHashMap<dynamic, dynamic> is not a more specific type of Map<String, dynamic> . – jamesdlin.


Video Answer


2 Answers

Try this:

Map<String, dynamic>.from(yourData) 
like image 62
diegoveloper Avatar answered Oct 02 '22 16:10

diegoveloper


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.

like image 28
jamesdlin Avatar answered Oct 02 '22 16:10

jamesdlin