Skip to main content
Optional parameters allow a function invocation to omit arguments for specific parameters. Dart supports optionality through two distinct parameter styles: optional positional parameters and optional named parameters.

Optional Positional Parameters

Optional positional parameters are declared by wrapping them in square brackets []. They must appear after all required positional parameters. Arguments for these parameters are mapped by their position. To provide an argument for a specific optional parameter, an argument must also be provided for every preceding optional parameter. Syntax:
void logMessage(String message, [String? priority, String? device]) {
  // Implementation
}

// Invocation
logMessage('System Start');                   // priority: null, device: null
logMessage('System Start', 'INFO');           // priority: 'INFO', device: null
logMessage('System Start', 'INFO', 'Mobile'); // priority: 'INFO', device: 'Mobile'

Optional Named Parameters

Named parameters are defined using curly braces {}. By default, named parameters are optional. When invoking the function, arguments are referenced by the parameter name, and the order of arguments does not matter. Syntax:
void configure({bool? verbose, bool? dryRun}) {
  // Implementation
}

// Invocation
configure(verbose: true, dryRun: false);
configure(dryRun: true); // verbose is null
configure();             // Both are null

The required Modifier

While named parameters are optional by default, the required modifier can be applied to force a parameter to be mandatory. A named parameter marked required is not optional; the compiler enforces its presence at the call site.
void authenticate({required String user, String? token}) {
  // Implementation
}

// Invocation
authenticate(user: 'admin'); // Valid; 'token' is optional
// authenticate(token: '123'); // Error: The named parameter 'user' is required.

Default Values

Both optional positional and optional named parameters can define default values using the = operator. If the argument is omitted during invocation, the parameter initializes with the provided default value. Constraints:
  1. Default values must be compile-time constants.
  2. If a default value is provided, the parameter type does not need to be nullable.
Syntax:
// Optional Positional with defaults
void connect(String host, [int port = 80]) {
  // ...
}

// Optional Named with defaults
void drawText({String font = 'Arial', int size = 12}) {
  // ...
}

Null Safety and Initialization

Dart’s sound null safety system enforces strict initialization rules for optional parameters:
  1. Non-nullable types: Must have a default value provided.
  2. Nullable types: May omit a default value (implicitly defaults to null) or provide a specific non-null default.
// Valid: Nullable type, implicit null default
void funcA([int? x]) {}

// Valid: Non-nullable type, explicit default
void funcB([int x = 0]) {}

// Invalid: Non-nullable type, no default
// void funcC([int x]) {} // Compile-time error
Master Dart with Deep Grasping Methodology!Learn More