Dart map()

Summary: in this tutorial, you’ll learn how to the Dart map() method to create a new iterable object containing elements that pass a test.

Introduction to the Dart map() method

The map() is a method of the Iterable class. The map() method creates a new iterable object that contains all elements of the iterable that pass a test.

The following shows the map() method:

Iterable<T> map<T>(T toElement(E e))Code language: Dart (dart)

The map() method iterates over elements of an iterable and passes each element to a function. If the function returns true, the map() method includes the elements in the result iterable object.

Since List and Set are iterable, you can call the map() method on these objects.

Dart map() method examples

Suppose that you have a list of salaries:

var salaries = [1000000.0, 125000.0, 150000.0];Code language: Dart (dart)

To create a new list where each salary is increased by 5%, you may use a for-in loop like this:

void main() {
  var salaries = [1000000.0, 125000.0, 150000.0];
  var newSalaries = [];
  for (var salary in salaries) {
    newSalaries.add(salary * 1.05);
  }
  print(newSalaries);
}Code language: Dart (dart)

The for-in loop works fine. However, it’s quite verbose.

The following example uses the map() method to return a new list in which each number is increased by 5%:

void main() {
  var salaries = [1000000.0, 125000.0, 150000.0];
  var newSalaries = salaries.map((salary) => salary * 1.05);
  print(newSalaries);
}Code language: Dart (dart)

Output:

(1050000.0, 131250.0, 157500.0)Code language: Dart (dart)

Unlike the for-in solution, the map() method returns an iterable, not a list.

If you want the result as a list, you can convert the iterable to a List using the toList() method as shown in the following example:

void main() {
  var salaries = [1000000.0, 125000.0, 150000.0];
  var newSalaries = salaries.map((salary) => salary * 1.05);
  print(newSalaries.toList());
}Code language: Dart (dart)

Output:

[1050000.0, 131250.0, 157500.0]Code language: Dart (dart)

Using the map() with the where() method

The following example uses the map() method to return an iterable of numbers (double) from a list of strings:

void main() {
  var inputs = ['1.24', '2.35', '4.56', 'abc'];
  var numbers = inputs.map((n) => double.tryParse(n));
  print(numbers);
}Code language: Dart (dart)

Output:

(1.24, 2.35, 4.56, null)Code language: Dart (dart)

Since the double.tryParse() method is the same as the function argument of the map() method, you can directly pass the double.tryParse method to the map() method instead of using an anonymous function:

void main() {
  var inputs = ['1.24', '2.35', '4.56', 'abc'];
  var numbers = inputs.map(double.tryParse);
  print(numbers);
}Code language: Dart (dart)

To remove null from the result, you can call the where() method after calling map() method like this:

void main() {
  var inputs = ['1.24', '2.35', '4.56', 'abc'];
  var numbers = inputs.map(double.tryParse).where((n) => n != null);
  print(numbers);
}Code language: Dart (dart)

Output:

(1.24, 2.35, 4.56)Code language: Dart (dart)

Because the map() method returns an iterable, we can immediately call the where() method of the iterable object.

Summary

  • Use the map() method to return an iterable of objects that pass a provided function.
Was this tutorial helpful ?