If I create a Map like this
Map<String, dynamic> map = Map<String, dynamic>();
In Documentation it says, It returns LinkedHashMap
/// Creates an empty [LinkedHashMap].
///
/// This constructor is equivalent to the non-const map literal `<K,V>{}`.
///
/// A `LinkedHashMap` requires the keys to implement compatible
/// `operator==` and `hashCode`.
/// It iterates in key insertion order.
external factory Map();
And the LinkedHashMap also an abstract class, we cannot create object for abstract class in dart.
abstract class LinkedHashMap<K, V> implements Map<K, V> {
/// Creates an insertion-ordered hash-table based [Map].
///
What is the actual instance we get in Map Constructor?
I am using the dart version 2.16.1 latest stable
CodePudding user response:
The Map default constructor is a factory constructor that makes a LinkedHashMap using its default constructor. The LinkedHashMap default constructor is also a factory constructor, and it makes an object of one of a few internal classes:
_InternalLinkedHashMap_CompactLinkedIdentityHashMap_CompactLinkedCustomHashMap
As for which one it chooses, that's based on what kind of equals, hashCode, and isValidKey methods were provided. For your case of calling Map(), it's going to be the first one, _InternalLinkedHashMap.
Source code
Here is the latest version (as of today) of the classes and constructors I mentioned:
- constructors: hash_factories.dart
_InternalLinkedHashMap: compact_hash.dart_CompactLinkedIdentityHashMap: compact_hash.dart_CompactLinkedCustomHashMap: compact_hash.dart
CodePudding user response:
What is the actual instance we get in Map Constructor?
The answer is LinkedHashMap. It's a concrete class, not an abstract class.
See LinkedHashMap.
The other example of the implementers of Map is HashMap.
