How to iterate elements with forEach in Dart
In Dart programming language, the forEach method is a way to iterate over the elements in an iterable collection, such as a list, map, or set. This method allows for the execution of a given function on each element in the collection without the need for an explicit loop.
The significance of forEach in Dart programming lies in its simplicity and versatility. It is a concise and straightforward method that can be used to perform a variety of actions on elements in a collection.
The forEach method takes a single parameter—a function applied to each element in the collection. The function takes one argument, which is the current element being processed. The syntax for the forEach method is as follows:
iterable.forEach((element) { ... });
The
forEachmethod is used to iterate over elements in a collection (represented by theiterable).The method takes an anonymous function as its argument, which is defined at the point of call.
The anonymous function takes one argument,
element, which represents the current element being processed from theiterableobject.The body of the anonymous function, defined within the curly braces
{ ... }, contains the action that will be performed on eachelementas it is processed from theiterableobject.
Use cases of the forEach loop
The forEach loop is one of several methods available in Dart for iterating over elements in a collection. It is important to understand when it is more appropriate to use forEach compared to other iteration methods like map and where.
The map method is used to transform each element in a collection into a new value, returning a new collection with the transformed values. The where method is used to filter elements in a collection based on a specified condition, returning a new collection with only the elements that satisfy the condition.
The forEach loop is more appropriate when the goal is to perform an action on each element in a collection, rather than transforming or filtering the elements. For example, we might use forEach to print each element in a list, or to increment a counter for each element in a set.
Examples with different data structures
To better understand the use of forEach in Dart, it is helpful to look at examples with different data structures. Here're examples of using forEach with a list, set, and map:
void main() {List<String> names = ['Ayo', 'David', 'Victoria', 'Helen'];names.forEach((name) {print(name);});}
Explanation
Line 2: We define a list of strings as
nameswith 4 elements.Lines 3–5: We call the
forEachmethod on thenameslist, passing an anonymous function as an argument. The anonymous function takes a single argumentnamewhich represents the current element being processed from thenameslist, and has a single statement:print(name)which prints thenameargument to the console.
Example
void main() {Set<int> numbers = {1, 2, 3};numbers.forEach((number) {print(number);});}
Explanation
Line 2: We define a set of integers as
numberswith the values, , . Lines 3–5: We call the
forEachmethod on thenumbersset, passing an anonymous function as an argument. The anonymous function takes a single argumentnumberwhich represents the current element being processed from thenumbersset, and has a single statement:print(number)which prints thenumberargument to the console.
Example
void main() {Map<String, int> ages = {'Ayo': 30, 'Samuel': 25, 'Emmanuel': 35};ages.forEach((name, age) {print('$name is $age years old');});}
Explanation
Line 2: We define a map as
ageswith key-value pairs'Ayo': 30, 'Samuel': 25, 'Emmanuel': 35.Lines 3–5: We call the
forEachmethod on theagesmap, passing an anonymous function as an argument. The anonymous function takes two argumentsnameandage, which represents the current key and value being processed from theagesmap respectively, and has a single statement:print('$name is $age years old')which prints thenameandagearguments to the console.
Example
void main() {List<Map<String, int>> grades = [{'Ayo': 60, 'Samuel': 89, 'Vic': 70},{'Ayo': 58, 'Samuel': 78, 'Vic': 58},{'Ayo': 86, 'Samuel': 70, 'Vic': 99}];int sum = 0;grades.forEach((studentGrades) {studentGrades.forEach((student, grade) {sum += grade;});});print('The sum of all grades is $sum');}
Explanation
Lines 2–6: We define a list of maps as
grades, where each map represents a set of studentnamesas keys and their respectivegradesas values.Lines 8–13: We initialize the variable named
sumto, then we use the forEachmethod to loop through the list of maps, where each map is represented by the variablestudentGrades. Within the innerforEach, each key-value pair withinstudentGradesis accessed, and the value is added to thesumvariable.Line 15: The sum of all grades is printed to the console.
Note: Avoid nested loops for better performance as it can slow down the code in worst case scenarios
Best practices and pitfalls to avoid
Maximize the use of forEach by following these best practices and avoiding these common pitfalls for optimal performance and efficiency.
Best practices
Use
forEachwhen we need to perform simple operations on each element in the iterable, such as printing or counting.Choose the right iterable type (e.g.
List,Set,Map) based on the operations we need to perform
Pitfalls to avoid
Avoid using nested loops as it can lead to performance issues and slow down the processing time.
forEachdoes not allow us to modify the iterable we are working on, so we'll use other methods such asmaporwhereto modify the data, if required.