Dart Function scoping
Dart functions may also be declared anonymously or nested. For example, to create a nestedfunction, just open a new function block within an existing function block
The function innerFunction may now be used inside, and only inside, outerFunction. No other other functions has access to it.
Functions in Dart may also be declared anonymously, which is commonly used as function arguments. A common example is the sort method of List object. This method takes an optional argument with the following signature:
The documentation states that the function must return 0 if the a and b are equal. It returns -1 if a <
b and 1 if a > b.
Knowing this, we can sort a list of integers using an anonymous function.
Anonymous function may also be bound to identifiers like so:
and used as an ordinary variable.