Skip to main content
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.

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

Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More