Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Arrow Functions


Share

Dart Arrow Functions

Many functions contain only one line of code that returns a value.

For these functions, Dart has a shorter way to write them, called the arrow syntax.

You use the arrow => instead of curly brackets and the return keyword.

This is a normal function that squares a number:

Example

int square(int x) {
  return x * x;
}

void main() {
  print(square(5));
}
Try it Yourself »

Result:

25

Here is the same function, written with the arrow syntax:

Example

int square(int x) => x * x;

void main() {
  print(square(5));
}
Try it Yourself »

Result:

25

The code => x * x; means the same as { return x * x; }.


Syntax

An arrow function has a return type, a name, parameters, the arrow, and one expression:

returnType functionName(parameters) => expression;

An expression is a piece of code that gives a value, like a + b or 'Hello $name'.

The value of the expression is returned automatically.

Do not write the return keyword after the arrow.

If you do, the program does not run:

Example

int add(int a, int b) => return a + b;

void main() {
  print(add(3, 4));
}
Try it Yourself »

Result:

main.dart:1:26: Error: Unexpected token 'return'.
int add(int a, int b) => return a + b;
                         ^^^^^^


More Examples

Arrow functions can take more than one parameter:

Example

int add(int a, int b) => a + b;

void main() {
  print(add(3, 4));
  print(add(10, 20));
}
Try it Yourself »

Result:

7
30

They can return a string:

Example

String greet(String name) => 'Hello, $name!';

void main() {
  print(greet('Liam'));
  print(greet('Emma'));
}
Try it Yourself »

Result:

Hello, Liam!
Hello, Emma!

They can also return a bool, which is useful for checks:

Example

bool isAdult(int age) => age >= 18;

void main() {
  print(isAdult(20));
  print(isAdult(15));
}
Try it Yourself »

Result:

true
false

You can use the conditional operator ? : to choose between two values in one expression:

Example

String grade(int score) => score >= 50 ? 'Pass' : 'Fail';

void main() {
  print(grade(75));
  print(grade(30));
}
Try it Yourself »

Result:

Pass
Fail

Void Arrow Functions

An arrow function can also have the return type void.

This is common when the function only calls print():

Example

void sayHello(String name) => print('Hello, $name');

void main() {
  sayHello('Olivia');
  sayHello('Noah');
}
Try it Yourself »

Result:

Hello, Olivia
Hello, Noah

Only One Expression

The arrow syntax only works with one expression.

You cannot put statements like if, for or a variable declaration after the arrow.

When a function needs more than one statement, use a normal function with curly brackets:

Example

String check(int x) {
  if (x > 0) {
    return 'positive';
  }
  return 'zero or negative';
}

void main() {
  print(check(5));
  print(check(-2));
}
Try it Yourself »

Result:

positive
zero or negative

Tip: Use the arrow syntax for short functions that fit on one line.

It makes your code shorter and easier to read.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.