AnnonymousFunction
Key points about anonymous functions:
Anonymous functions can take zero or more parameters.
They are defined using the
=>
syntax.They are often used for short, single-expression functions.
Let's explore some examples of anonymous functions in Dart:
Basic Anonymous Function:
In this example, we define an anonymous function assigned to the variable add
. It takes two int
parameters a
and b
and returns their sum.
Anonymous Function as an Argument:
Anonymous functions are commonly used as arguments in higher-order functions like forEach
, map
, and filter
. Here's an example using the forEach
method:
In this example, an anonymous function is passed as an argument to forEach
. The anonymous function takes each number
from the list and prints it.
Arrow Function as an Anonymous Function:
Arrow functions are often used for very concise anonymous functions. Here's an example of an arrow function as an anonymous function:
In this case, the anonymous function takes an integer x
and returns its double.
Use of Anonymous Function in Sorting:
You can use anonymous functions to define custom comparison logic for sorting. For example, sorting a list of strings by length:
In this example, we use an anonymous function as a comparator to sort the list of names by their lengths.
Anonymous functions are versatile and allow you to write concise, inline functions for various purposes in your Dart code. They are particularly useful when you need short, throwaway functions for specific tasks.
Last updated