🛠 Flutter world will be ready by the end of September, 2024 🚀🔥
Learn Dart Programming
Dart Basics
Anonymous Functions in Dart

Anonymous function in Dart

Functions without a name are called anonymous functions. Anonymous functions can be assigned to a variable or passed as an argument to another function.

Example:

 
void main() {
  var list = ['apples', 'bananas', 'oranges'];
  list.forEach((item) {
    print('${list.indexOf(item)}: $item');
  });
}
 

Output:


0: apples

1: bananas

2: oranges

Here we have used an anonymous function as an argument to the forEach() method. The anonymous function takes a single argument, item, and prints the index of the item in the list and the item itself.

Arrow function in Dart

Arrow functions are a short-hand syntax for defining anonymous functions.

Example:

 
void main() {
  var list = ['apples', 'bananas', 'oranges'];
  list.forEach((item) => print('${list.indexOf(item)}: $item'));
}
 

Output:


0: apples

1: bananas

2: oranges

Here we have used an arrow function as an argument to the forEach() method. The arrow function takes a single argument, item, and prints the index of the item in the list and the item itself.

Function as a parameter in Dart

In Dart, functions are first-class objects. This means that functions can be assigned to a variable, passed as an argument to another function, and returned from another function.

Example:

 
void main() {
  var list = ['apples', 'bananas', 'oranges'];
  list.forEach(printFruit);
}
 
void printFruit(item) {
  print('${list.indexOf(item)}: $item');
}
 

Output:

0: apples
 
1: bananas
 
2: oranges
 

Here we have passed the printFruit() function as an argument to the forEach() method. The printFruit() function takes a single argument, item, and prints the index of the item in the list and the item itself.

Function as a return value in Dart

In Dart, functions are first-class objects. This means that functions can be assigned to a variable, passed as an argument to another function, and returned from another function.

Example:

 
void main() {
  var list = ['apples', 'bananas', 'oranges'];
  var fruit = getFruit();
  print(fruit);
}
 
String getFruit() {
  return 'apples';
}
 

Output:


apples

Here we have assigned the getFruit() function to a variable, fruit. The getFruit() function returns a string, apples, which is then printed to the console.

Function as a closure in Dart

A closure is a function object that has access to variables in its lexical scope, even when the function is used outside of its original scope.

Example:

 
void main() {
  var list = ['apples', 'bananas', 'oranges'];
  var fruit = getFruit();
  print(fruit);
}
 
String getFruit() {
  return 'apples';
}
 

Output:


apples

Here lexical scope means the scope of the function. In the above example, the getFruit() function has access to the list variable, even though it is declared outside of the function. This is because the getFruit() function is a closure.

Conclusion

In this article, we have learned about functions in Dart. We have learned about the different types of functions in Dart. We have also learned about anonymous functions, arrow functions, functions as a parameter, functions as a return value, and functions as a closure in Dart.