Skip to main content

Introduction

Named parameters allow you to specify arguments by name when calling a function, making your code more readable and allowing you to provide parameters in any order.

Named Parameter Syntax

Wrap parameters in curly braces {} and use the required keyword for mandatory parameters:
void crearUsuario({
  required nombre, 
  required edad, 
  required rol
}) {
  print('Usuario: $nombre, Edad: $edad, Rol: $rol');
}

Key Components

  • Curly braces: {} - indicate named parameters
  • required keyword: Makes the parameter mandatory
  • Parameter name: Used when calling the function

Calling with Named Parameters

Use the parameter name followed by a colon and the value:
crearUsuario(nombre: 'Luis', edad: 25, rol: 'Admin');
// Output: Usuario: Luis, Edad: 25, Rol: Admin

Order Flexibility

The main advantage of named parameters is that you can provide them in any order:
crearUsuario(rol: 'admin', edad: 19, nombre: 'Laura');
// Output: Usuario: Laura, Edad: 19, Rol: admin
Both calls work identically despite the different parameter order.

Complete Example

void crearUsuario({
  required nombre, 
  required edad, 
  required rol
}) {
  print('Usuario: $nombre, Edad: $edad, Rol: $rol');
}

void main() {
  crearUsuario(nombre: 'Luis', edad: 25, rol: 'Admin');
  crearUsuario(rol: 'admin', edad: 19, nombre: 'Laura');
  crearUsuario(nombre: 15, edad: 'Juan', rol: 'Admin');
}
In the example above, the third call has mismatched types (nombre: 15, edad: 'Juan'). While Dart allows dynamic typing without explicit type annotations, it’s better to specify types for better type safety.

Named Parameters with Types

For better code quality, always specify parameter types:
void crearUsuario({
  required String nombre,
  required int edad,
  required String rol
}) {
  print('Usuario: $nombre, Edad: $edad, Rol: $rol');
}

void main() {
  crearUsuario(nombre: 'Luis', edad: 25, rol: 'Admin');
}

Optional Named Parameters

Named parameters can be optional by omitting required and providing default values:
void configurarApp({
  String tema = 'claro',
  bool notificaciones = true,
  String idioma = 'español'
}) {
  print('Tema: $tema');
  print('Notificaciones: $notificaciones');
  print('Idioma: $idioma');
}

void main() {
  configurarApp();
  configurarApp(tema: 'oscuro');
  configurarApp(tema: 'oscuro', idioma: 'inglés');
}

Mixed Required and Optional

Combine required and optional named parameters:
void registrarProducto({
  required String nombre,
  required double precio,
  String categoria = 'General',
  bool disponible = true
}) {
  print('Producto: $nombre');
  print('Precio: \$${precio}');
  print('Categoría: $categoria');
  print('Disponible: $disponible');
}

void main() {
  registrarProducto(
    nombre: 'Laptop',
    precio: 15000.00
  );
  
  registrarProducto(
    nombre: 'Mouse',
    precio: 250.00,
    categoria: 'Accesorios'
  );
}

Nullable Named Parameters

Use nullable types for optional parameters without defaults:
void crearContacto({
  required String nombre,
  String? telefono,
  String? email
}) {
  print('Nombre: $nombre');
  if (telefono != null) print('Teléfono: $telefono');
  if (email != null) print('Email: $email');
}

void main() {
  crearContacto(nombre: 'Ana');
  crearContacto(nombre: 'Carlos', email: '[email protected]');
  crearContacto(
    nombre: 'María',
    telefono: '555-1234',
    email: '[email protected]'
  );
}

Benefits of Named Parameters

Readability

Function calls are self-documenting
crear(nombre: 'Ana', edad: 25);

Flexibility

Provide arguments in any order
crear(edad: 25, nombre: 'Ana');

Safety

Required parameters are enforced
// Error if missing required params

Extensibility

Easy to add optional parameters later
// Add new optional params easily

When to Use Named Parameters

  • Functions with more than 2-3 parameters
  • When parameter order isn’t intuitive
  • When you want to make some parameters optional
  • For configuration or builder-style functions
  • When parameter names add clarity

Positional vs Named Parameters

AspectPositionalNamed
Syntaxfunc(a, b)func(a: 1, b: 2)
OrderMust matchAny order
Optional[param]Omit required
ClarityLess clearSelf-documenting
SkippableNoYes

Best Practices

  • Always use named parameters for functions with 3+ parameters
  • Mark truly required parameters with required
  • Provide sensible defaults for optional named parameters
  • Use type annotations for better type safety
  • Choose clear, descriptive parameter names
Don’t mix positional optional parameters [] and named parameters {} in the same function. Choose one style or the other.

Advanced Pattern

Combine positional required parameters with named optional parameters:
void procesarPedido(
  String id,  // Positional required
  {
    String? notas,  // Named optional
    bool urgente = false,  // Named optional with default
  }
) {
  print('Pedido ID: $id');
  print('Urgente: $urgente');
  if (notas != null) print('Notas: $notas');
}

void main() {
  procesarPedido('ORD-123');
  procesarPedido('ORD-456', urgente: true);
  procesarPedido('ORD-789', urgente: true, notas: 'Entregar antes de las 5pm');
}

Build docs developers (and LLMs) love