Dart Map.fromIterables()
Syntax & Examples
Syntax of Map.fromIterables
The syntax of Map.Map.fromIterables constructor is:
Map.fromIterables(Iterable<K> keys, Iterable<V> values)
This Map.fromIterables constructor of Map creates a map associating the given keys
to the given values
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
keys | required | An iterable representing the keys to be associated with the values. |
values | required | An iterable representing the values to be associated with the keys. |
✐ Examples
1 Create a map from string-int pairs
In this example,
- We create an iterable of keys containing strings 'a', 'b', 'c' and an iterable of values containing integers 1, 2, 3.
- We then create a map using
Map.fromIterables()
with the provided keys and values iterables. - The resulting map associates the keys with their corresponding values.
- We print the map to standard output.
Dart Program
void main() {
var keys = ['a', 'b', 'c'];
var values = [1, 2, 3];
var map = Map.fromIterables(keys, values);
print('Map from iterables: $map');
}
Output
Map from iterables: {a: 1, b: 2, c: 3}
2 Create a map from int-string pairs
In this example,
- We create an iterable of keys containing integers 1, 2, 3 and an iterable of values containing strings 'one', 'two', 'three'.
- We then create a map using
Map.fromIterables()
with the provided keys and values iterables. - The resulting map associates the keys with their corresponding values.
- We print the map to standard output.
Dart Program
void main() {
var keys = [1, 2, 3];
var values = ['one', 'two', 'three'];
var map = Map.fromIterables(keys, values);
print('Map from iterables: $map');
}
Output
Map from iterables: {1: one, 2: two, 3: three}
3 Create a map from boolean-string pairs
In this example,
- We create an iterable of keys containing booleans true, false and an iterable of values containing strings 'yes', 'no'.
- We then create a map using
Map.fromIterables()
with the provided keys and values iterables. - The resulting map associates the keys with their corresponding values.
- We print the map to standard output.
Dart Program
void main() {
var keys = [true, false];
var values = ['yes', 'no'];
var map = Map.fromIterables(keys, values);
print('Map from iterables: $map');
}
Output
Map from iterables: {true: yes, false: no}
Summary
In this Dart tutorial, we learned about Map.fromIterables constructor of Map: the syntax and few working examples with output and detailed explanation for each example.