Dart Arrow Functions

Writing concise and elegant single-expression functions

➡️ What are Arrow Functions?

Arrow functions in Dart provide a concise way to write single-expression functions using the => syntax. They make code shorter and more readable for simple operations.


// Traditional function
int add(int a, int b) {
  return a + b;
}

// Arrow function (shorter)
int addArrow(int a, int b) => a + b;

print(addArrow(5, 3)); // Output: 8
                                    

Output:

8

Arrow Function Benefits

Concise Syntax

Less code for simple functions

// Before
bool isEven(int n) {
  return n % 2 == 0;
}

// After
bool isEven(int n) => n % 2 == 0;
📖

Readable

Clear and easy to understand

String greet(String name) => 'Hello, $name!';
double square(double x) => x * x;
bool isEmpty(String text) => text.length == 0;
🎯

Single Expression

Perfect for one-line operations

int max(int a, int b) => a > b ? a : b;
String upper(String text) => text.toUpperCase();
double area(double r) => 3.14159 * r * r;
🔄

Functional Style

Great with map, where, etc.

var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.map((n) => n * 2);
var evens = numbers.where((n) => n % 2 == 0);

🔹 Basic Arrow Functions

Convert regular functions to arrow functions:

// Mathematical operations
int multiply(int a, int b) => a * b;
double divide(double a, double b) => a / b;
int absolute(int n) => n < 0 ? -n : n;

// String operations
String capitalize(String text) => text.isEmpty ? '' : 
    text[0].toUpperCase() + text.substring(1);
int getLength(String text) => text.length;
bool contains(String text, String pattern) => text.contains(pattern);

void main() {
  print('5 * 3 = ${multiply(5, 3)}');
  print('10 / 3 = ${divide(10, 3)}');
  print('Absolute of -7: ${absolute(-7)}');
  print('Capitalize "hello": ${capitalize("hello")}');
}

Output:

5 * 3 = 15

10 / 3 = 3.3333333333333335

Absolute of -7: 7

Capitalize "hello": Hello

🔹 Arrow Functions with Collections

Arrow functions work great with list operations:

void main() {
  var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  
  // Transform data
  var squares = numbers.map((n) => n * n).toList();
  var doubled = numbers.map((n) => n * 2).toList();
  
  // Filter data
  var evens = numbers.where((n) => n % 2 == 0).toList();
  var greaterThan5 = numbers.where((n) => n > 5).toList();
  
  // Check conditions
  bool hasEven = numbers.any((n) => n % 2 == 0);
  bool allPositive = numbers.every((n) => n > 0);
  
  print('Squares: $squares');
  print('Doubled: $doubled');
  print('Evens: $evens');
  print('Greater than 5: $greaterThan5');
  print('Has even number: $hasEven');
  print('All positive: $allPositive');
}

Output:

Squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Doubled: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Evens: [2, 4, 6, 8, 10]

Greater than 5: [6, 7, 8, 9, 10]

Has even number: true

All positive: true

🔹 Arrow Functions vs Regular Functions

When to use arrow functions and when to use regular functions:

// ✅ Good for arrow functions (single expression)
String formatName(String first, String last) => '$first $last';
bool isAdult(int age) => age >= 18;
double calculateTax(double amount) => amount * 0.08;

// ❌ Not suitable for arrow functions (multiple statements)
void processUser(String name, int age) {
  print('Processing user: $name');
  if (age < 18) {
    print('User is a minor');
  } else {
    print('User is an adult');
  }
  print('Processing complete');
}

// ✅ Arrow function with conditional expression
String getAgeGroup(int age) => age < 18 ? 'Minor' : 
                               age < 65 ? 'Adult' : 'Senior';

void main() {
  print(formatName('John', 'Doe'));
  print('Is 25 adult? ${isAdult(25)}');
  print('Tax on \$100: \$${calculateTax(100)}');
  print('Age group for 30: ${getAgeGroup(30)}');
  
  processUser('Alice', 16);
}

Output:

John Doe

Is 25 adult? true

Tax on $100: $8.0

Age group for 30: Adult

Processing user: Alice
User is a minor
Processing complete

🧠 Test Your Knowledge

What symbol is used to create arrow functions in Dart?