dart foreach loop

There are two types of dart foreach loop one with “in” operator another with forEach method.

 

Type 1 : dart foreach loop with IN operator

List<String> languages=["c","js","dart"];//way to declare DART array

main(){
 for(String eachLang in languages){
   print(eachLang);
 }
}

At the first line, we are declaring an array (in DART array is List) of type String and we added three string in the list. In the line no 4 we are using a foreach loop with in operator in every iteration it will take one element from its right  side array/list ( languages )  and put it in the variable declared in the left side (eachLang)

play with dart foreach loop [hit the red play button to run] 

Type 2 (a) : dart foreach loop with forEach method

forEach is not a Loop this is an inbuilt method available in List class which is very handy to iterate all the elements in a List.

List<String> language=["c","js","dart"];//way to declare DART array

main(){
 language.forEach(printData);
}

// A function that accept only one parameter 
printData(String data){
 print(data);
}

In Line 1 we have declared a DART List (array) of three items. So the variable language is of type List and it can only hold String elements. As language is of type list than it has a method named forEach (defined in List class)

forEach function takes a special function as an argument. The speciality of the method is it should accept only one parameter. (in DART a function is an object) . So in Line 8, we have declared a function named printData which accepts only one parameter and we sent this function (printData) as an argument in forEach (in line 4 )

play with dart forEach loop [hit the red play button to run] 

Isn’t it too much to loop through the variables we have to declare a new function 🙁

 

Type 2 (b): simplified dart foreach loop with forEach method and anonymous function

Anonymous functions are functions which don’t have any name in other words if you declare a function without a name than it is an Anonymous function.( same as all other languages)

List<String> language=["c","js","dart"];//way to declare DART array

main(){
 language.forEach((data){
   // this is an anonymous function : a function without name
   print(data);
 });
}

In line 4 you can see how we have declared a function without giving its name, but using this technic you don’t have to create a new function when you want to use forEach method to loop.

play with dart forEach loop with anonymous function [hit the red play button to run]

Type 2 (c): further simplification of  dart foreach loop with forEach method and fat arrow notation

List<String> language=["c","js","dart"];//way to declare DART array

main(){
 language.forEach((data) =>print(data));
}

 

What you have learned

  1. How to declare a DART array
  2. How to use forEach loop in dart  using in operator
  3. How to use forEach loop in dart  using forEach method of List object
  4. How to use forEach loop in dart  using forEach method with anonymous function and fat arrow notation
  5. Learned how forEach method works

DART forEach official documentation

Leave a Reply

Your email address will not be published. Required fields are marked *