Skip to content
DeveloperMemos

Understanding Fat Arrow Notation in Dart

Dart, Programming, Syntax1 min read

For developers who are keen on enhancing their proficiency in Dart, understanding its syntax and diverse notations is paramount. One of the essential features that aid in writing concise and expressive code is the fat arrow notation. In this article, we will explore the significance of the fat arrow notation in Dart and provide practical examples to illustrate its usage.

What is Fat Arrow Notation?

In Dart, the fat arrow (=>) notation is a shorthand syntax for defining single-line functions or methods. It simplifies the syntax required for writing small and straightforward functions, making the code more concise and readable. The fat arrow notation is particularly useful when creating short functions, such as those used as arguments to higher-order functions like map() or forEach().

Using Fat Arrow Notation

Let's delve into some practical examples to understand the application of fat arrow notation in Dart.

Example 1: Single-Line Function

Suppose we have a simple function that doubles a given integer. Traditionally, we would define the function using the following syntax:

1int doubleValue(int number) {
2 return number * 2;
3}

Using fat arrow notation, the above function can be rewritten in a more concise manner:

1int doubleValue(int number) => number * 2;

The fat arrow notation eliminates the need for the curly braces and the return keyword, resulting in a cleaner and more compact representation of the function.

Example 2: Using with Higher-Order Functions

Another scenario where fat arrow notation shines is when working with higher-order functions. Consider the following example, where we have a list of integers and we want to create a new list containing the squares of these integers:

1void main() {
2 List<int> numbers = [1, 2, 3, 4, 5];
3
4 // Using traditional function syntax
5 List<int> squares = numbers.map((int number) {
6 return number * number;
7 }).toList();
8
9 // Using fat arrow notation
10 List<int> squaresShortened = numbers.map((int number) => number * number).toList();
11}

As demonstrated in the above example, fat arrow notation allows us to succinctly express the transformation logic within the map() function without the need for additional boilerplate code.

Benefits of Fat Arrow Notation

The utilization of fat arrow notation offers several advantages, including:

  • Conciseness: It reduces the verbosity of code by eliminating unnecessary keywords and braces, thereby enhancing code readability.
  • Clarity: It provides a more straightforward representation of simple functions, making code intent more apparent.
  • Reduced Typing: With fat arrow notation, developers can write code faster by minimizing the amount of code required for defining concise functions.

By leveraging fat arrow notation, developers can make their Dart code more elegant and maintainable, especially when dealing with shorter functions and methods.