Open In App

Dart – Anonymous Functions

An anonymous function in Dart is like a named function but they do not have names associated with it. An anonymous function can have zero or more parameters with optional type annotations. An anonymous function consists of self-contained blocks of code and that can be passed around in our code as a function parameter.

Syntax:



(parameter_list)

{
  



     statement(s)

}

Example: 




// Dartprogram to illustrate 
// Anonymous functions in Dart
void main()
{
  var list = ["Shubham","Nick","Adil","Puthal"];
  print("GeeksforGeeks - Anonymous function in Dart");
  list.forEach((item) {
    print('${list.indexOf(item)} : $item');
  });
}

 

Output:

This example defines an anonymous function with an untyped parameter, item. The function, invoked for each item in the list, prints a string that includes the value at the specified index. 

Article Tags :