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.

Optional positional parameters in Dart are function parameters that do not require an argument to be passed by the caller. They are identified strictly by their position in the function signature rather than by a parameter name, and are defined by enclosing them within a single set of square brackets [].

Syntax and Placement

Optional positional parameters must be declared after all required positional parameters. A function can have multiple optional positional parameters, but they must all be grouped within a single set of square brackets at the end of the parameter list. Additionally, optional positional parameters are mutually exclusive with named parameters ({}). A function signature can declare either optional positional parameters or named parameters, but it cannot combine both.
// 'a' is required. 'b' and 'c' are optional positional parameters.
void execute(int a, [int? b, int? c]) {
  // Function body
}

// COMPILATION ERROR: Expected ')' before this. (Cannot precede required parameters)
void invalidSignature([int? a], int b) {} 

// COMPILATION ERROR: Cannot have both optional positional and named parameters.
void invalidCombination([int? a], {int? b}) {}

Nullability and Default Values

Because the caller can omit these arguments, Dart’s sound null safety enforces strict typing rules for optional parameters. An optional positional parameter must either be declared as a nullable type (such as appending the ? suffix, or by using the inherently nullable dynamic type) or be assigned a constant default value using the = operator. Nullable Type: If no default value is provided and the argument is omitted, the parameter initializes to null.
void process(String input, [String? encoding, dynamic context]) {
  // If omitted, both 'encoding' and 'context' are null.
}
Default Value: If a default value is provided, the parameter can be non-nullable. The default value must be a compile-time constant.
void process(String input, [String encoding = 'utf-8']) {
  // If omitted, 'encoding' is 'utf-8'.
}

Sequential Assignment

Arguments passed to optional positional parameters are evaluated and assigned strictly in the order they are declared. It is impossible to skip an earlier optional parameter to provide a value for a later one.
void configure(String env, [int? timeout, bool? verbose]) {}

// Valid: Assigns 'env' and 'timeout'. 'verbose' remains null.
configure('production', 5000);

// Invalid logic: Attempting to pass 'verbose' without 'timeout'.
// The compiler will attempt to assign the boolean `true` to the integer `timeout`, 
// resulting in a type mismatch error.
configure('production', true); 
To provide an argument for the nth optional positional parameter, the caller must provide arguments for all n-1 preceding optional parameters.
Master Dart with Deep Grasping Methodology!Learn More