Skip to content
tezvyn:

Dart Function Syntax: Block Body vs. Arrow Notation

Source: dart.devEasyHow cards are made

In Dart, use a block body {} for multiple statements or => for one expression. For example, bool isEven(int n) => n % 2 == 0; returns the expression directly. Arrow syntax cannot contain a sequence of statements.

Why it exists

To give developers both a robust, multi-line function syntax for complex logic and a concise, single-line syntax for simple expressions. This duality allows for both power and improved code readability depending on the task's complexity.

The mental model

Think of Dart function syntax as having two modes. The default is a block body, enclosed in curly braces {}, which can contain multiple statements and requires an explicit return. For functions that only compute and return a single expression, you can use the arrow => as a direct shorthand. The arrow syntax is syntactic sugar for { return expression; }.

How it works

A standard function uses a block body. For example: bool isNoble(int atomicNumber) { return _nobleGases[atomicNumber] != null; }. This form requires the return keyword. The arrow syntax provides a more compact alternative for functions containing only one expression. The previous example becomes: bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null;. The => symbol replaces the curly braces and the return keyword; the result of the expression is automatically returned.

When to use it

Use arrow syntax for simple, single-expression functions. It is perfect for simple calculations, boolean checks, or getters that return a computed value. It makes code less verbose and easier to read when the logic is straightforward. Use the standard block syntax for any function that requires more than one statement, such as declaring local variables, performing conditional logic before returning, or executing side effects.

When not to use it

Do not use arrow syntax if your function needs to perform multiple operations, declare intermediate variables, or include control flow like if-else blocks. Forcing complex logic into a single, long line with arrow syntax makes code unreadable and hard to debug. Stick to block bodies for clarity when logic is not trivial.

One canonical example

Let's define a function to check if a number is even. Using the standard block syntax, you would write: bool isEven(int number) { return number % 2 == 0; }. Using the equivalent arrow syntax, it becomes much more concise: bool isEven(int number) => number % 2 == 0;. Both functions are identical in behavior.

Interview question

What is the primary characteristic that makes Dart's arrow function syntax (=>) suitable for a function?

  • a.It implicitly returns the result of a single, concise expression.Correct
  • b.It allows for the declaration of intermediate variables within the function scope.
  • c.It enables the function to execute multiple statements sequentially.
  • d.It explicitly requires the return keyword for better readability.
Why?

Arrow syntax is designed for functions that compute and return the result of a single expression, implicitly handling the return. Options A and B describe scenarios where a block body function is required, while option D incorrectly states that arrow syntax requires an explicit return keyword.

Just read this? Test yourself on what you have been reading.

Read the original → dart.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles