Dart map function
last modified October 18, 2026
In this article we show how to map elements of an Iterable to a function in Dart language. In our examples, we use the list collection.
The map function returns a lazy Iterable which is
created by calling the specified function on each element of an
Iterable. The function is called only when the result is iterated,
and it is called again each time the result is iterated again. To get a
concrete collection, we call toList or toSet.
Do not confuse the Iterable.map method with the Map
collection type.
Dart map simple example
The following is a simple map function example.
void main() {
final vals = [1, 2, 3, 4, 5, 6];
final res = vals.map((e) => e * 5);
print(res);
print(res.toList());
}
We have a list of integers. We apply the map function on each of the elements.
The function is a lambda expression, which multiplies an element by 5. The
first print shows the lazy iterable in parentheses; the second
shows a list created with toList.
$ dart main.dart (5, 10, 15, 20, 25, 30) [5, 10, 15, 20, 25, 30]
Dart map example II
In the following example, we apply the map function on a list of
words.
void main() {
final words = ['sky', 'cloud', 'snow', 'summer', 'cup', 'water'];
final lengths = words.map((e) => e.length);
print(lengths);
}
In the program, we transform a list of words into a sequence of word lengths. The mapped type may differ from the element type: strings become integers.
$ dart main.dart (3, 5, 4, 6, 3, 5)
Dart map with a tear-off
If a function or a method already has the right signature, we can pass it directly instead of writing a lambda expression.
void main() {
final words = ['sky', 'cloud', 'snow'];
print(words.map((w) => w.toUpperCase()).toList());
final nums = ['1', '2', '3'];
print(nums.map(int.parse).toList());
print(nums.map(int.parse).reduce((a, b) => a + b));
}
The int.parse static method is passed as a tear-off, which is
shorter than (s) => int.parse(s).
$ dart main.dart [SKY, CLOUD, SNOW] [1, 2, 3] 6
Dart map is lazy
Because map returns a lazy iterable, the function is not called
until the elements are requested. The following example makes this visible.
void main() {
final res = [1, 2, 3].map((e) {
print('mapping $e');
return e * 2;
});
print('map called');
print(res.first);
print(res.toList());
}
Nothing is printed by the lambda until res.first is evaluated; at
that moment only the first element is mapped. The call to toList
iterates the whole iterable, so the function is called for all three elements
again. Store the result with toList if the mapping is expensive and
the result is used repeatedly.
$ dart main.dart map called mapping 1 2 mapping 1 mapping 2 mapping 3 [2, 4, 6]
Dart indexed map function
If we want to work with element indexes, we can use the indexed
property of an iterable. It yields records of the form
(index, element).
void main() {
final words = ['sky', 'cloud', 'snow', 'summer', 'cup', 'water'];
final res = words.indexed.map((e) => '{idx=${e.$1}, len=${e.$2.length}}');
print(res);
}
We map the list elements into an iterable of strings, which contain the index
value and the string length. The record fields are accessed with
$1 and $2.
$ dart main.dart
({idx=0, len=3}, {idx=1, len=5}, {idx=2, len=4}, ..., {idx=4, len=3}, {idx=5, len=5})
The collection package offers the mapIndexed
extension method, which passes the index and the element as two parameters.
We add the package to the project.
$ dart pub add collection
import 'package:collection/collection.dart';
void main() {
final words = ['sky', 'cloud', 'snow', 'summer', 'cup', 'water'];
final res =
words.mapIndexed((idx, word) => '{idx=$idx, len=${word.length}}');
print(res);
}
The result is the same, but the lambda takes the index and the element as separate parameters.
$ dart main.dart
({idx=0, len=3}, {idx=1, len=5}, {idx=2, len=4}, ..., {idx=4, len=3}, {idx=5, len=5})
Dart map projection
A projection is a selection of specific fields from the returned objects.
class User {
final String firstName;
final String lastName;
final int salary;
User(this.firstName, this.lastName, this.salary);
@override
String toString() => '$firstName|$lastName|$salary';
}
void main() {
final users = <User>[
User('John', 'Doe', 1230),
User('Lucy', 'Novak', 670),
User('Ben', 'Walter', 2050),
User('Robin', 'Brown', 2300),
User('Amy', 'Doe', 1250),
User('Joe', 'Draker', 1190),
User('Janet', 'Doe', 980),
User('Albert', 'Novak', 1930),
];
final salaries = users.map((e) => e.salary);
print(salaries);
final names = users.map((e) => '${e.firstName} ${e.lastName}');
print(names.take(3).toList());
}
We have a list of users having three fields: first name, last name, and salary. We pick the salary field to form a new lazy iterable. The second projection builds full names.
$ dart main.dart (1230, 670, 2050, 2300, 1250, 1190, 980, 1930) [John Doe, Lucy Novak, Ben Walter]
Dart map to records
Since Dart 3, records are a convenient way to project several fields at once without declaring a new class.
class User {
final String firstName;
final String lastName;
final int salary;
User(this.firstName, this.lastName, this.salary);
}
void main() {
final users = [
User('John', 'Doe', 1230),
User('Lucy', 'Novak', 670),
User('Ben', 'Walter', 2050),
];
final res = users.map((u) => (name: u.lastName, salary: u.salary));
for (final (:name, :salary) in res) {
print('$name earns $salary');
}
}
The lambda returns a record with two named fields. In the loop, we destructure each record with a record pattern.
$ dart main.dart Doe earns 1230 Novak earns 670 Walter earns 2050
Source
Dart map method - API reference
In this article we have called the map function on elements
of a list collection.
Author
List all Dart tutorials.