The syntax of Map.Map.unmodifiable constructor is:
Map.unmodifiable(Map other)
This Map.unmodifiable constructor of Map creates an unmodifiable hash-based map containing the entries of other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | The map whose entries will be included in the new unmodifiable map. |
In this example,
map1
containing integer-string pairs.Map.unmodifiable()
with map1
as the argument.map1
.void main() {
var map1 = {1: 'one', 2: 'two', 3: 'three'};
var unmodifiableMap = Map.unmodifiable(map1);
print('Unmodifiable map: $unmodifiableMap');
}
Unmodifiable map: {1: one, 2: two, 3: three}
In this example,
map2
containing string-integer pairs.Map.unmodifiable()
with map2
as the argument.map2
.void main() {
var map2 = {'a': 1, 'b': 2, 'c': 3};
var unmodifiableMap = Map.unmodifiable(map2);
print('Unmodifiable map: $unmodifiableMap');
}
Unmodifiable map: {a: 1, b: 2, c: 3}
In this example,
map3
containing string-string pairs.Map.unmodifiable()
with map3
as the argument.map3
.void main() {
var map3 = {'x': 'apple', 'y': 'banana', 'z': 'cherry'};
var unmodifiableMap = Map.unmodifiable(map3);
print('Unmodifiable map: $unmodifiableMap');
}
Unmodifiable map: {x: apple, y: banana, z: cherry}
In this Dart tutorial, we learned about Map.unmodifiable constructor of Map: the syntax and few working examples with output and detailed explanation for each example.