For Each Loop In Dart

For Each Loop

The for each loop iterates over all list elements or variables. It is useful when you want to loop through list/collection. The syntax of for-each loop is:

collection.forEach(void f(value));

Example

void main() 
{
       var num = [1, 2, 3, 4, 5, 6];
       num.forEach((var item) 
       {
           if (item % 2 == 0) 
           {
               //checks for even numbers in num list
               var myList = [item];
               print(myList); //print even numbers
            }
       });

}