The `map` method in Dart returns a new lazy iterable with elements that are created by applying a function to each element of the original iterable.
The syntax of Runes.map() method is:
Iterable<T> map<T>(T f(int e))
This map() method of Runes returns a new lazy Iterable with elements that are created by calling f
on each element of this Iterable
in iteration order.
Parameter | Optional/Required | Description |
---|---|---|
f | required | A function that takes an integer element as input and returns a new element of type T. |
Runes.map() returns value of type Iterable<T>
.
In this example,
runes
from the string 'Hello'.map
method to apply a function that returns the length of each character as a new element.void main() {
Runes runes = Runes('Hello');
Iterable<int> lengths = runes.map((element) => element.toString().length);
print('Lengths of each character: $lengths');
}
Lengths of each character: (2, 3, 3, 3, 3)
In this example,
numbers
from the string '12345'.map
method to apply a function that doubles each number and converts it to a string.void main() {
Runes numbers = Runes('12345');
Iterable<String> doubledNumbers = numbers.map((element) => (element * 2).toString());
print('Doubled numbers: $doubledNumbers');
}
Doubled numbers: (98, 100, 102, 104, 106)
In this Dart tutorial, we learned about map() method of Runes: the syntax and few working examples with output and detailed explanation for each example.