Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Anonymous functions in Dart

development programming #dart #lambda #anonymous-functions #syntax

Sometimes we need anonymous functions for one-off tasks. Dart allows two similar syntaxes for writing these.

The first one is used when there is a single expression to write:

(a, b) => a + b

The other is used when you need to write multiple statements in an anonymous function:

(a, b) { 
  return a + b; 
}