Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A required named parameter in Dart is a named function or constructor parameter that mandates an explicit argument at the call site. Enforced by the required modifier, it guarantees that a value is passed during invocation, allowing the parameter to be strongly typed as non-nullable without necessitating a default value.

Syntax

Required named parameters are declared inside curly braces {} using the required keyword preceding the type declaration.
void configureConnection({required String host, required int port}) {
  print('Connecting to $host:$port');
}

// Valid invocation (order independent)
configureConnection(port: 8080, host: 'localhost');

// Invalid invocation (compile-time error: missing 'port')
// configureConnection(host: 'localhost'); 

Technical Mechanics

  • Compile-Time Enforcement: The Dart analyzer strictly enforces the presence of the parameter. Failing to provide a required named parameter results in a compile-time error, not a runtime exception.
  • Null Safety Interaction: By default, named parameters in Dart are optional. Under sound null safety, optional parameters must either be nullable (e.g., String?) or have a default value. The required modifier overrides this, allowing a named parameter to be non-nullable (e.g., String) without a default value.
  • Explicit Nullability: A parameter can be both required and nullable (Type?). In this scenario, the caller is still forced to explicitly pass an argument, even if that argument is explicitly null.
  • Position Independence: Like all named parameters, required named parameters do not rely on positional arguments and can be passed in any order at the call site.
  • Mixing Parameter Types: Required named parameters can be freely mixed with optional named parameters within the same {} block.

Advanced Declaration Example

void executeOperation(
  String operationId, {          // Positional parameter
  required String environment,   // Required, non-nullable
  required String? authKey,      // Required, explicitly nullable
  int timeout = 3000,            // Optional, default value provided
}) {
  // Function body
}

// Caller must explicitly provide 'environment' and 'authKey'
executeOperation(
  'op-123',
  authKey: null, 
  environment: 'production',
);
Master Dart with Deep Grasping Methodology!Learn More