> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# Named Parameters

> Learn how to use named parameters in Dart functions

## 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:

```dart theme={null}
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:

```dart theme={null}
crearUsuario(nombre: 'Luis', edad: 25, rol: 'Admin');
// Output: Usuario: Luis, Edad: 25, Rol: Admin
```

## Order Flexibility

<Note>
  The main advantage of named parameters is that you can provide them in any order:
</Note>

```dart theme={null}
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

```dart theme={null}
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');
}
```

<Warning>
  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.
</Warning>

## Named Parameters with Types

For better code quality, always specify parameter types:

```dart theme={null}
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:

```dart theme={null}
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:

```dart theme={null}
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:

```dart theme={null}
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: 'carlos@example.com');
  crearContacto(
    nombre: 'María',
    telefono: '555-1234',
    email: 'maria@example.com'
  );
}
```

## Benefits of Named Parameters

<CardGroup cols={2}>
  <Card title="Readability" icon="eye">
    Function calls are self-documenting

    ```dart theme={null}
    crear(nombre: 'Ana', edad: 25);
    ```
  </Card>

  <Card title="Flexibility" icon="shuffle">
    Provide arguments in any order

    ```dart theme={null}
    crear(edad: 25, nombre: 'Ana');
    ```
  </Card>

  <Card title="Safety" icon="shield">
    Required parameters are enforced

    ```dart theme={null}
    // Error if missing required params
    ```
  </Card>

  <Card title="Extensibility" icon="plus">
    Easy to add optional parameters later

    ```dart theme={null}
    // Add new optional params easily
    ```
  </Card>
</CardGroup>

## When to Use Named Parameters

<Check>
  * 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
</Check>

## Positional vs Named Parameters

| Aspect    | Positional   | Named              |
| --------- | ------------ | ------------------ |
| Syntax    | `func(a, b)` | `func(a: 1, b: 2)` |
| Order     | Must match   | Any order          |
| Optional  | `[param]`    | Omit `required`    |
| Clarity   | Less clear   | Self-documenting   |
| Skippable | No           | Yes                |

## Best Practices

<Tip>
  * 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
</Tip>

<Warning>
  Don't mix positional optional parameters `[]` and named parameters `{}` in the same function. Choose one style or the other.
</Warning>

## Advanced Pattern

Combine positional required parameters with named optional parameters:

```dart theme={null}
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');
}
```
