Skip to main content
Named parameters are function parameters identified by their label during invocation rather than their ordinal position. Defined by enclosing the parameter list in curly braces {}, named parameters are optional by default unless explicitly annotated with the required modifier.

Declaration Syntax

To define named parameters, wrap the specific parameters within curly braces in the function signature.
void updateSettings({bool? isVisible, int? timeout}) {
  // Function body
}

Invocation Syntax

When calling a function with named parameters, you must specify the parameter name followed by a colon (:) and the value. The order of arguments does not matter.
// Valid calls
updateSettings(isVisible: true, timeout: 500);
updateSettings(timeout: 500, isVisible: true);
updateSettings(isVisible: false); // 'timeout' remains null

Null Safety and Default Values

Because named parameters are optional by default, the Dart analyzer enforces null safety rules to prevent runtime errors. There are three states a named parameter can exist in:
  1. Nullable: The type is explicitly marked with ?, allowing the value to be null if the caller omits the argument.
  2. Default Value: A default value is provided using =, used if the caller omits the argument.
  3. Required: The parameter is annotated with required, mandating that the caller provide a value.
void configure({
  String? label,            // Nullable: defaults to null if omitted
  int retries = 3,          // Default value: defaults to 3 if omitted
  required String host,     // Required: Compiler error if omitted
}) { 
  print('$host, $label, $retries');
}

Mixing Positional and Named Parameters

A function can accept both positional and named parameters. However, all positional parameters must be declared before the named parameters.
// 'id' is positional; 'force' and 'verbose' are named
void deleteItem(int id, {bool force = false, bool? verbose}) {
  // ...
}

void main() {
  deleteItem(101, force: true);
}
Master Dart with Deep Grasping Methodology!Learn More