Dart - how _InternalLinkedHashMap<String, dynamic> convert to Map<String, dynamic>?
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.
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