The syntax of Map.Map.from constructor is:
Map.from(Map other)
This Map.from constructor of Map creates a LinkedHashMap
with the same keys and values as other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the map to copy keys and values from |
In this example,
other
containing integer keys and string values.Map.from()
constructor to create a new map copiedMap
with the same keys and values as other
.void main() {
Map<int, String> other = {1: 'one', 2: 'two', 3: 'three'};
Map<int, String> copiedMap = Map.from(other);
print(copiedMap);
}
{1: one, 2: two, 3: three}
In this example,
other
containing string keys and integer values.Map.from()
constructor to create a new map copiedMap
with the same keys and values as other
.void main() {
Map<String, int> other = {'a': 1, 'b': 2, 'c': 3};
Map<String, int> copiedMap = Map.from(other);
print(copiedMap);
}
{a: 1, b: 2, c: 3}
In this example,
other
containing string keys and string values.Map.from()
constructor to create a new map copiedMap
with the same keys and values as other
.void main() {
Map<String, String> other = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'};
Map<String, String> copiedMap = Map.from(other);
print(copiedMap);
}
{key1: value1, key2: value2, key3: value3}
In this Dart tutorial, we learned about Map.from constructor of Map: the syntax and few working examples with output and detailed explanation for each example.