Skip to main content
Positional parameters are function parameters where the arguments passed during invocation are mapped to the parameters based strictly on their sequential order in the function signature. In Dart, positional parameters are divided into two categories: mandatory and optional.

Mandatory Positional Parameters

By default, all parameters declared in a Dart function are mandatory positional parameters. The caller must provide an argument for every parameter, and the types must match the sequence defined in the signature.
void updateCoordinates(int x, int y, int z) {
  // Implementation
}

// Valid invocation: Arguments map strictly to x, y, and z respectively.
updateCoordinates(10, 20, 30); 

Optional Positional Parameters

Optional positional parameters are enclosed in square brackets []. They allow the caller to omit arguments at the end of the parameter list. Because Dart enforces null safety, an optional parameter must either be declared as a nullable type (using ?) or be assigned a default value using the = operator. Default values must be compile-time constants.
void configureNetwork(String protocol, [String? ipAddress, int port = 80]) {
  // 'protocol' is mandatory.
  // 'ipAddress' is optional, defaults to null.
  // 'port' is optional, defaults to 80.
}

// Valid invocations:
configureNetwork('TCP'); 
configureNetwork('TCP', '192.168.1.1'); 
configureNetwork('TCP', '192.168.1.1', 8080); 

Technical Constraints

  1. Declaration Order: Optional positional parameters must always be declared after all mandatory positional parameters.
  2. Sequential Resolution: Because they are positional, you cannot skip an optional parameter. To provide an argument for the second optional parameter, you must provide an argument for the first optional parameter.
  3. Mutual Exclusivity: A function signature cannot contain both optional positional parameters [] and named parameters {}. You must choose one or the other for the optional segment of the signature.
// INVALID: Optional positional parameters must be at the end.
void invalidFunction([int a = 0], int b) {}

// INVALID: Cannot mix optional positional and named parameters.
void mixedFunction(int a, [int b = 0], {int c = 0}) {}
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More