The syntax of Map.Map.of constructor is:
Map.of(Map<K, V> other)
This Map.of constructor of Map creates a LinkedHashMap
with the same keys and values as other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | The map whose keys and values will be used to create the new map. |
In this example,
map1
containing integer-string pairs.Map.of()
with map1
as the argument.map1
.void main() {
var map1 = {1: 'one', 2: 'two', 3: 'three'};
var newMap = Map.of(map1);
print('New map: $newMap');
}
New map: {1: one, 2: two, 3: three}
In this example,
map2
containing string-integer pairs.Map.of()
with map2
as the argument.map2
.void main() {
var map2 = {'a': 1, 'b': 2, 'c': 3};
var newMap = Map.of(map2);
print('New map: $newMap');
}
New map: {a: 1, b: 2, c: 3}
In this example,
map3
containing string-string pairs.Map.of()
with map3
as the argument.map3
.void main() {
var map3 = {'x': 'apple', 'y': 'banana', 'z': 'cherry'};
var newMap = Map.of(map3);
print('New map: $newMap');
}
New map: {x: apple, y: banana, z: cherry}
In this Dart tutorial, we learned about Map.of constructor of Map: the syntax and few working examples with output and detailed explanation for each example.