Introduction
Arrow functions (also called fat arrow syntax) provide a concise way to write functions that contain a single expression. They’re perfect for simple operations and callbacks.
Arrow Function Syntax
Use the => operator to create a single-expression function:
int multiplicarFlecha ( int a, int b) => a * b;
Key Components
Return type : int - same as traditional functions
Function name : multiplicarFlecha
Parameters : (int a, int b) - same as traditional functions
Arrow operator : => - replaces the curly braces and return keyword
Expression : a * b - automatically returned
Traditional vs Arrow Syntax
Traditional Function
int multiplicar ( int a, int b) {
return a * b;
}
Arrow Function
int multiplicarFlecha ( int a, int b) => a * b;
Both functions do exactly the same thing, but the arrow function is more concise.
Complete Example
// Forma tradicional
int multiplicar ( int a, int b) {
return a * b;
}
// Forma flecha (para una sola línea)
int multiplicarFlecha ( int a, int b) => a * b;
void main () {
print ( multiplicar ( 4 , 5 )); // Salida: 20
//Llamar la Función de flecha o Arrow Functions
print ( multiplicarFlecha ( 4 , 5 )); // Salida: 20
}
When to Use Arrow Functions
Arrow functions are ideal for:
Single-expression functions
Simple calculations
Getters and simple methods
Callback functions
Mapping and filtering operations
Common Use Cases
Mathematical Operations
int sumar ( int a, int b) => a + b;
int restar ( int a, int b) => a - b;
double dividir ( int a, int b) => a / b;
int modulo ( int a, int b) => a % b;
String Operations
String saludar ( String nombre) => 'Hola, $ nombre ' ;
String mayusculas ( String texto) => texto. toUpperCase ();
int longitud ( String texto) => texto.length;
Boolean Operations
bool esPar ( int numero) => numero % 2 == 0 ;
bool esPositivo ( int numero) => numero > 0 ;
bool esVacio ( String texto) => texto.isEmpty;
bool esMayor ( int a, int b) => a > b;
Void Functions
Arrow functions work with void return types too:
void imprimir ( String mensaje) => print (mensaje);
void saludar () => print ( 'Hola mundo' );
void despedir ( String nombre) => print ( 'Adiós, $ nombre ' );
Arrow Functions with Named Parameters
int calcular ({
required int a,
required int b,
required String operacion
}) => operacion == 'suma' ? a + b : a - b;
void main () {
print ( calcular (a : 10 , b : 5 , operacion : 'suma' )); // 15
print ( calcular (a : 10 , b : 5 , operacion : 'resta' )); // 5
}
Arrow Functions in Collections
With Lists
void main () {
var numeros = [ 1 , 2 , 3 , 4 , 5 ];
// Map with arrow function
var dobles = numeros. map ((n) => n * 2 );
print (dobles. toList ()); // [2, 4, 6, 8, 10]
// Filter with arrow function
var pares = numeros. where ((n) => n % 2 == 0 );
print (pares. toList ()); // [2, 4]
}
With forEach
void main () {
var nombres = [ 'Ana' , 'Carlos' , 'Diana' ];
// Traditional
nombres. forEach ((nombre) => print ( 'Hola, $ nombre ' ));
// Even more concise
nombres. forEach (print);
}
Limitations of Arrow Functions
Arrow functions can only contain a single expression. You cannot:
Use multiple statements
Declare local variables
Use control flow statements (if, for, while)
Have multiple lines of code
Won’t Work
// ❌ Error: Multiple statements
int calcular ( int a, int b) => {
int resultado = a + b;
return resultado;
};
// ❌ Error: Control flow
int absoluto ( int n) => {
if (n < 0 ) return - n;
return n;
};
Will Work
// ✅ Single expression with ternary
int absoluto ( int n) => n < 0 ? - n : n;
// ✅ Single expression
int calcular ( int a, int b) => a + b;
Getters with Arrow Syntax
Arrow functions are commonly used for getters:
class Rectangulo {
final double ancho;
final double alto;
Rectangulo ( this .ancho, this .alto);
// Arrow function getter
double get area => ancho * alto;
double get perimetro => 2 * (ancho + alto);
bool get esCuadrado => ancho == alto;
}
void main () {
var rect = Rectangulo ( 5 , 10 );
print (rect.area); // 50.0
print (rect.perimetro); // 30.0
print (rect.esCuadrado); // false
}
Comparison Chart
Traditional Functions Use when:
Multiple statements needed
Complex logic required
Local variables needed
Better for debugging
int func ( int x) {
var result = x * 2 ;
return result + 1 ;
}
Arrow Functions Use when:
Single expression
Simple calculations
Callbacks
Conciseness matters
int func ( int x) => x * 2 + 1 ;
Best Practices
Use arrow functions for simple, single-expression operations
Keep expressions short and readable
Don’t sacrifice readability for brevity
Use traditional syntax when logic becomes complex
Perfect for functional programming patterns
Good arrow function examples:
Getters and setters
Simple calculations
Callbacks and lambdas
Mapping and filtering
Converting or transforming data
Arrow functions and traditional functions have the same performance. The arrow syntax is purely syntactic sugar for writing more concise code.