Dart Basics

Get an introduction to the basics of the Dart programming language, used for development with the Flutter SDK for mobile, web and beyond. By Jonathan Sande.

4.6 (19) · 2 Reviews

Download materials
Save for later
Share
You are currently viewing page 5 of 5 of this article. Click here to view the first page.

Anonymous Functions

Dart supports first-class functions, meaning that it treats functions like any other data type. You can assign them to variables, pass them as arguments and return them from other functions.

To pass these functions around as values, omit the function name and return type. Since there’s no name, this type of function is called an anonymous function.

Anonymous function

You can assign an anonymous function to a variable named onPressed, like so:

final onPressed = () {
  print('button pressed');
};

onPressed has a value of type Function. The empty parentheses indicate the function has no parameters. Like regular functions, the code inside the curly brackets is the function body.

To execute the code within the function body, call the variable name as if it were the name of the function:

onPressed(); // button pressed

You can simplify functions whose body only contains a single line by using arrow syntax. To do this, remove the curly brackets and add a fat arrow =>.

Here’s a comparison of the anonymous function above and a refactored version:

// original anonymous function
final onPressed = () {
  print('button pressed');
};

// refactored
final onPressed = () => print('button pressed');

That’s much nicer to read.

Using Anonymous Functions

You’ll often see anonymous functions in Flutter, like those above, passed around as callbacks for UI events. This lets you specify the code that runs when a user does something, like pressing a button.

Another common place where you’ll see anonymous functions is with collections. You can give a collection an anonymous function that will perform some task on each element of the collection. For example:

// 1
final drinks = ['water', 'juice', 'milk'];
// 2
final bigDrinks = drinks.map(
  // 3
  (drink) => drink.toUpperCase()
);
// 4
print(bigDrinks); // (WATER, JUICE, MILK)

Let’s look at each step:

  1. Define a list of drinks that have lowercase letters.
  2. .map takes all the list values and returns a new collection with them.
  3. An anonymous function is passed as a parameter. In that anonymous function, you have a drink argument that represents each element of the list.
  4. The body of the anonymous function converts each element to uppercase and returns the value. Since the original list is a list of strings, drink also has type String.

Using an anonymous function and combining it with .map is a convenient way to transform one collection into another.

Note: Don’t confuse the .map method with the Map type.

Run the code to see the resulting collection.

Anonymous functions

Congratulations, you’ve finished the tutorial. You should now have a better understanding of the Dart code you see when learning how to build Flutter apps!

Where to Go From Here?

You can download the code examples using the Download Materials button at the top or bottom of this tutorial.

If you’re ready to dive into Flutter, check out our Getting Started With Flutter tutorial.

For a more comprehensive look at Flutter, read our book, Flutter Apprentice.

You can go beyond the basics and learn more about Object-Oriented Programming and asynchronous programming with our book, Dart Apprentice.

Also, check out the official Dart documentation.

We hope you enjoyed this brief introduction to Dart basics. If you have any questions or comments, please join the forum discussion below.