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?
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.
_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.
Try this:
Map<String, dynamic>.from(yourData)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With