[]). They cannot be applied to mandatory parameters.
Syntax and Mechanics
The assignment operator (=) is used to declare a default value.
Named Optional Parameters
When defining named parameters, the default value is specified directly after the parameter declaration.Positional Optional Parameters
For positional parameters, the default value is assigned within the square brackets. Positional optional parameters must appear after all required positional parameters.Technical Constraints
-
Compile-Time Constants: Default values must be compile-time constants. You cannot use variables, instance properties, or function calls evaluated at runtime as default values.
-
Implicit Null Default: If an optional parameter is declared without a default value, its implicit default is
null. Consequently, the parameter’s type signature must be explicitly nullable (using the?suffix) to satisfy Dart’s sound null safety. -
Collection Defaults: When providing a default value that is a collection (like a
List,Set, orMap), the collection literal must be explicitly marked asconstto satisfy the compile-time constant requirement. -
Mutual Exclusivity with
required: A named parameter cannot simultaneously possess a default value and therequiredmodifier. Therequiredkeyword mandates that the caller provides an argument, rendering a fallback default value logically dead code.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





