Skip to main content
Named parameters are function arguments explicitly bound to a parameter identifier at the call site, rather than relying on their ordinal position in the argument list. In Dart, they are declared by enclosing the parameter definitions within curly braces {} in the function signature.

Syntax and Declaration

To define named parameters, place them inside {} at the end of the parameter list. At the call site, they are invoked using the parameterName: value syntax.
// Declaration
void configure({String? host, int port = 80}) {
  // Function body
}

void main() {
  // Invocation
  configure(port: 443, host: '127.0.0.1');
}

Technical Characteristics

1. Optionality and Nullability By default, all named parameters are optional. Because they can be omitted during invocation, Dart’s sound null safety requires that an optional named parameter must either be declared as a nullable type (using the ? suffix) or be assigned a default value. 2. Default Values Default values are assigned using the = operator in the function signature. The assigned default value must be a compile-time constant.
void setFlags({bool isVerbose = false, String? logPath}) {}
3. The required Modifier To mandate that a caller provides a specific named parameter, prefix the parameter declaration with the required keyword. A required named parameter bypasses the default optionality rule, meaning it does not need to be nullable or possess a default value, as the analyzer guarantees its presence at compile time.
void initialize({required String apiKey, required int environmentId}) {}
4. Ordering and Interleaving At the call site, named arguments can be passed in any arbitrary order, completely independent of their declaration order in the function signature. Furthermore, as of Dart 2.17, named arguments can be interleaved with positional arguments at the call site, provided the positional arguments maintain their strict ordinal sequence.
void processData(int id, String payload, {required bool encrypt, int retries = 3}) {}

void main() {
  // Valid: Named arguments placed after positional arguments
  processData(101, 'data', encrypt: true, retries: 5);

  // Valid: Named arguments interleaved with positional arguments (Dart 2.17+)
  processData(101, encrypt: true, 'data', retries: 5);
  
  // Valid: Arbitrary ordering of the named arguments themselves
  processData(101, 'data', retries: 5, encrypt: true);
}
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More