---
title: "Returning Values with Arrow Functions and Expression Bodies in Dart"
date: "2026-12-03T10:05:00+07:00"
description: "Dart's => arrow syntax is shorthand for { return expr; }. Learn where expression bodies shine, when to use block bodies instead, and how tear-offs beat lambdas."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-arrow-functions-expression-bodies.jpg"
alt: "Conceptual illustration of a curly brace block collapsing into a single sleek arrow pointing at a value"
tags: ['dart', 'dart-language', 'functions', 'syntax', 'readability']
---

Dart lets you compress a whole function into a single line with `=>`, and once you see it you'll notice it everywhere — in Flutter builders, collection callbacks, and getters. It's tiny, but knowing exactly when it helps (and when it hurts) separates tidy code from cryptic code.

## => is shorthand for { return expr; }

The language docs state it directly: the `=> expr` syntax is shorthand for `{ return expr; }`. This arrow notation is often called *arrow syntax*:

```dart
// These are exactly equivalent
bool isNoble(int atomicNumber) {
  return _nobleGases[atomicNumber] != null;
}

bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null;
```

There is no behavioral difference — only a stylistic one. The arrow form keeps a one-expression function on a single line and puts the reader's eye straight on the result.

## Only expressions can follow the arrow

The key constraint: only *expressions* may appear between `=>` and the `;`. Expressions evaluate to values, so you can't write a statement where Dart expects a value. You cannot put an `if` statement after an arrow — you use a conditional expression instead:

```dart
// Wrong — an if statement is not an expression
String grade(int score) => if (score >= 90) 'A' else 'B';

// Right — conditional expression
String grade(int score) => score >= 90 ? 'A' : 'B';
```

The same goes for loops and declarations. If you find yourself wanting statements after `=>`, you want a block body.

## Arrow syntax works on every kind of function

The arrow form isn't limited to anonymous callbacks. It works for top-level functions, methods, getters, setters, local functions, and anonymous functions alike:

```dart
// Top-level function
int doubleIt(int n) => n * 2;

// Method
class Counter {
  int count = 0;
  void increment() => count++; // Expression-bodied void method.
}

// Anonymous function / lambda
final loudify = (msg) => '!!! ${msg.toUpperCase()} !!!';
```

That last line comes straight from the docs — an anonymous function with a single return expression collapses to `(msg) => ...`, which you'll write constantly as a callback. Note also that arrow syntax never changes a function's *type*: a `=>`-style method and its block-style twin are interchangeable everywhere a function value is expected, including `typedef`s, tear-offs, and overrides.

## Expression-bodied getters and setters

Computed properties are the natural home for `=>`. A getter that calculates a value reads best as one expression, and a small setter can forward in one line too:

```dart
class Box {
  double left = 0, right = 100, top = 0, bottom = 50;
  num x = 0;

  double get area => (right - left) * (bottom - top);

  num get position => center.x;
  set position(num value) => center = Point(value, center.y);
}
```

Effective Dart even pairs them: a `get x => ...` alongside `set x(value) => ...` keeps a property's read and write paths visually symmetric. This is also the shape of "avoid storing what you can calculate" — derive `area` on demand instead of caching a field that can go stale.

## When NOT to use an arrow body

People love `=>` — Effective Dart warns it's *very easy to abuse* and end up with code that's hard to read. If your declaration is more than a couple of lines or contains deeply nested expressions, use a block body:

```dart
// Good — readable block body
Treasure? openChest(Chest chest, Point where) {
  if (_opened.containsKey(chest)) return null;

  var treasure = Treasure(where);
  treasure.addAll(chest.contents);
  _opened[chest] = treasure;
  return treasure;
}

// Hard to read — nested ternary and cascade after =>
Treasure? openChest(Chest chest, Point where) => _opened.containsKey(chest)
    ? null
    : _opened[chest] = (Treasure(where)..addAll(chest.contents));
```

Side-effecting functions are another candidate for block bodies: a `void save()` with logging, flushing, and cleanup deserves statements, not one chained expression. The same applies to constructors, `main()`, and any function where a reader would have to mentally unwind a chain of ternaries and cascades to follow the control flow. When in doubt, clarity beats brevity — you'll read this code far more often than you write it.

## Callbacks, arrow functions, and tear-offs

Function expressions with `=>` shine as inline callbacks:

```dart
var names = people.map((person) => person.name);
var evens = numbers.where((n) => n.isEven).toList();
```

But before writing an arrow lambda that merely forwards arguments, check for a tear-off. Effective Dart says **DON'T** create a lambda when a tear-off will do — referring to a function without parentheses already produces a closure with the same parameters, so the wrapper adds noise and an extra closure allocation for zero benefit:

```dart
// Good — tear-offs
charCodes.forEach(print);
charCodes.forEach(buffer.write);

// Unnecessary lambdas
charCodes.forEach((code) => print(code));
charCodes.forEach((code) => buffer.write(code));
```

For the full story on tear-offs, closures, and function equality, see the companion post `dart-closures-function-tear-offs.md`.

## A quick readability guide

| Body style | Example | Use it when |
|---|---|---|
| Block | `int f() { return 1; }` | Multiple statements, early returns, side effects |
| Arrow | `int f() => 1;` | Exactly one expression, ideally one line |
| Arrow getter | `double get area => w * h;` | Simple computed properties |
| Arrow callback | `list.map((e) => e.name)` | Trivial transformation in a pipeline |
| Tear-off | `list.forEach(print)` | Lambda would only forward arguments |

Effective Dart's rule is **CONSIDER** using `=>` for simple members — "consider," because the judgment call is yours. The linter rule behind it is `prefer_expression_function_bodies`, which nudges simple declarations toward the arrow form while leaving complex ones alone. Run `dart analyze` with that lint enabled and it will flag block bodies that could be expressions, quietly training the whole team's eye for when the arrow reads better.

## Takeaways

`=>` means "return this expression," works on every function form, and is ideal for one-line methods, getters, and trivial callbacks. It only accepts expressions — reach for conditional expressions rather than statements — and when a body grows past a couple of lines, a block body serves your future readers better. And when the lambda would do nothing but forward arguments, prefer a tear-off.

**Takeaway:** Use `=>` when the whole body is one clear expression, switch to a block body the moment statements or nesting appear, and reach for a tear-off before writing a forwarding lambda.

## References

- https://dart.dev/language/functions
- https://dart.dev/effective-dart/usage
- https://dart.dev/effective-dart/design
