ZetCode

Dart Arrow Functions

last modified June 4, 2025

In Dart, arrow functions provide a concise syntax for writing function expressions. They are also known as lambda functions or anonymous functions.

Arrow functions use the => syntax to define a function that executes a single expression and returns its result. They are particularly useful for short functions and callbacks.

Basic Arrow Function Syntax

The simplest arrow function takes parameters and returns an expression. The => syntax replaces both the curly braces and return statement.

main.dart
void main() {
  // Traditional function
  int add(int a, int b) {
    return a + b;
  }
  
  // Arrow function equivalent
  int addArrow(int a, int b) => a + b;
  
  print(add(5, 3));      // 8
  print(addArrow(5, 3)); // 8
}

Both functions perform the same addition operation. The arrow version is more concise. Note that arrow functions can only contain a single expression.

$ dart main.dart
8
8

Arrow Functions with Collections

Arrow functions are commonly used with collection methods like map and where.

main.dart
void main() {
  var numbers = [1, 2, 3, 4, 5];
  
  // Using arrow function with map
  var squares = numbers.map((n) => n * n).toList();
  print(squares); // [1, 4, 9, 16, 25]
  
  // Using arrow function with where
  var evens = numbers.where((n) => n % 2 == 0).toList();
  print(evens);   // [2, 4]
}

The map method transforms each element, while where filters elements. Arrow functions make these operations concise and readable.

$ dart main.dart
[1, 4, 9, 16, 25]
[2, 4]

Arrow Functions as Callbacks

Arrow functions work well as callback functions for event handlers and timers.

main.dart
void main() {
  // Timer with traditional callback
  Timer(Duration(seconds: 1), () {
    print('Timer finished!');
  });
  
  // Timer with arrow function callback
  Timer(Duration(seconds: 2), () => print('Arrow timer finished!'));
  
  // Simulating async operation
  Future.delayed(Duration(seconds: 1), () => 'Hello')
    .then((value) => print(value));
}

All three examples use callback functions. The arrow versions are more compact while maintaining clarity. The Future.delayed example chains arrow functions.

$ dart main.dart
Timer finished!
Hello
Arrow timer finished!

Arrow Functions with Named Parameters

Arrow functions can work with named parameters, providing flexibility.

main.dart
void main() {
  // Function with named parameters
  String greet({required String name, String title = 'Mr.'}) =>
      'Hello, $title $name!';
  
  print(greet(name: 'Alice', title: 'Ms.')); // Hello, Ms. Alice!
  print(greet(name: 'Bob'));                 // Hello, Mr. Bob!
  
  // Function with optional positional parameters
  String join(List<String> parts, [String separator = ' ']) =>
      parts.join(separator);
  
  print(join(['one', 'two', 'three']));       // one two three
  print(join(['one', 'two', 'three'], '-')); // one-two-three
}

Both examples show arrow functions with different parameter types. The first uses named parameters with defaults, while the second uses optional positional ones.

$ dart main.dart
Hello, Ms. Alice!
Hello, Mr. Bob!
one two three
one-two-three

Arrow Functions in Class Methods

Arrow syntax can be used for concise class method definitions.

main.dart
class Point {
  final double x;
  final double y;
  
  Point(this.x, this.y);
  
  // Arrow function method
  double distanceTo(Point other) => 
      sqrt(pow(x - other.x, 2) + pow(y - other.y, 2));
  
  // Getter with arrow syntax
  String get description => 'Point($x, $y)';
}

void main() {
  var p1 = Point(0, 0);
  var p2 = Point(3, 4);
  
  print(p1.description);          // Point(0, 0)
  print(p2.description);          // Point(3, 4)
  print(p1.distanceTo(p2));       // 5.0
}

The Point class uses arrow syntax for both a method and a getter. This makes the class definition more compact while keeping it readable.

$ dart main.dart
Point(0, 0)
Point(3, 4)
5.0

Best Practices

Source

Dart Function Documentation

This tutorial covered Dart's arrow functions with practical examples demonstrating their syntax and common usage patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.