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:

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:

Order Flexibility

The main advantage of named parameters is that you can provide them in any order:
Both calls work identically despite the different parameter order.

Complete Example

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:

Optional Named Parameters

Named parameters can be optional by omitting required and providing default values:

Mixed Required and Optional

Combine required and optional named parameters:

Nullable Named Parameters

Use nullable types for optional parameters without defaults:

Benefits of Named Parameters

Readability

Function calls are self-documenting

Flexibility

Provide arguments in any order

Safety

Required parameters are enforced

Extensibility

Easy to add optional parameters later

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

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: