Skip to main content

Introduction

Optional parameters allow you to call functions with or without providing all arguments. This makes functions more flexible when some values aren’t always needed.

Optional Parameter Syntax

Wrap optional parameters in square brackets [] and provide default values:
void mostrarInfo(String nombre, [String ciudad = 'Sin Ciudad']) {
  print('Nombre: $nombre, Ciudad: $ciudad');
}

Key Components

  • Required parameter: String nombre - must always be provided
  • Square brackets: [] - indicate optional parameters
  • Default value: = 'Sin Ciudad' - used when the parameter is not provided

Using Optional Parameters

With Default Value

When you don’t provide the optional parameter, the default value is used:
mostrarInfo('Juan');
// Output: Nombre: Juan, Ciudad: Sin Ciudad

With Provided Value

When you provide the optional parameter, your value overrides the default:
mostrarInfo('Ana', 'Ciudad de México');
// Output: Nombre: Ana, Ciudad: Ciudad de México

Complete Example

// Parámetros opcionales entre []
void mostrarInfo(String nombre, [String ciudad = 'Sin Ciudad']) {
  print('Nombre: $nombre, Ciudad: $ciudad');
}

void main() {
  mostrarInfo('Juan');                    // Ciudad: Sin Ciudad
  mostrarInfo('Ana', 'Ciudad de México');  // Ciudad: Ciudad de México
}

Multiple Optional Parameters

You can have multiple optional parameters:
void crearPerfil(
  String nombre, 
  [String ciudad = 'Desconocida', 
   int edad = 0,
   String pais = 'México']
) {
  print('Nombre: $nombre');
  print('Ciudad: $ciudad');
  print('Edad: $edad');
  print('País: $pais');
}

void main() {
  crearPerfil('Carlos');
  crearPerfil('Ana', 'Guadalajara');
  crearPerfil('Luis', 'Monterrey', 30);
  crearPerfil('María', 'Puebla', 25, 'México');
}
Optional positional parameters must be provided in order. You cannot skip an optional parameter and provide a later one.

Default Values

String Defaults

void saludar([String mensaje = 'Hola']) {
  print(mensaje);
}

Numeric Defaults

void calcular([int cantidad = 1, double precio = 0.0]) {
  print('Total: ${cantidad * precio}');
}

Boolean Defaults

void configurar([bool activo = true]) {
  print('Estado: ${activo ? "Activo" : "Inactivo"}');
}

Nullable Optional Parameters

You can make optional parameters nullable without default values:
void mostrarDatos(String nombre, [String? apellido]) {
  if (apellido != null) {
    print('Nombre completo: $nombre $apellido');
  } else {
    print('Nombre: $nombre');
  }
}

void main() {
  mostrarDatos('Juan');
  mostrarDatos('Ana', 'García');
}
Use the ? suffix to make a parameter nullable when you don’t want to provide a default value.

Optional vs Required

Required Parameters

  • Must be provided when calling the function
  • Come before optional parameters
  • No square brackets needed
void func(String required) { }

Optional Parameters

  • Can be omitted when calling the function
  • Come after required parameters
  • Enclosed in square brackets
void func([String? optional]) { }

When to Use Optional Parameters

  • When a parameter has a sensible default value
  • When some function features are only needed occasionally
  • To maintain backward compatibility when adding new parameters
  • To reduce the number of function overloads

Best Practices

  • Always provide meaningful default values
  • Don’t create too many optional parameters (3 or fewer is ideal)
  • Consider using named parameters instead if you have many optional parameters
  • Document what the default values mean
If you need to skip optional parameters in the middle, consider using named parameters instead of positional optional parameters.

Comparison Table

AspectRequiredOptional Positional
SyntaxType name[Type name = default]
OrderMust matchMust match
SkippableNoNo (without later ones)
DefaultNoneRequired
NullableWith ?With ? or default

Build docs developers (and LLMs) love