The `forEach` method in Dart applies the function f to each element of this collection in iteration order.
The syntax of Runes.forEach() method is:
void forEach(void f(int element))
This forEach() method of Runes applies the function f
to each element of this collection in iteration order.
Parameter | Optional/Required | Description |
---|---|---|
f | required | The function to apply to each element, taking an integer element as its argument. |
Runes.forEach() returns value of type void
.
In this example,
numbers
containing integers.forEach()
method with a function that prints each element.void main() {
List<int> numbers = [1, 2, 3];
numbers.forEach((element) => print(element));
}
1 2 3
In this example,
words
containing strings.forEach()
method with a function that prints the uppercase version of each word.void main() {
List<String> words = ['hello', 'world'];
words.forEach((word) => print(word.toUpperCase()));
}
HELLO WORLD
In this example,
uniqueNumbers
containing integers.forEach()
method with a function that prints each number multiplied by 2.void main() {
Set<int> uniqueNumbers = {1, 2, 3};
uniqueNumbers.forEach((number) => print(number * 2));
}
2 4 6
In this Dart tutorial, we learned about forEach() method of Runes: the syntax and few working examples with output and detailed explanation for each example.