this. prefix, it performs the assignment implicitly during the initialization phase, before the constructor body executes.
Syntax
The syntax replaces the standard type and parameter name withthis. followed by the name of the instance variable.
Technical Mechanics
- Implicit Initialization: The parameter value is assigned to the instance variable as part of the initializer list sequence. This occurs before the constructor body runs.
- Type Inference: The parameter implicitly inherits the type of the instance variable it initializes. While explicit type annotation is valid (e.g.,
Vector2(double this.x)), it is redundant and generally omitted. - Scope: The parameter name is available within the initializer list and the constructor body, shadowing the instance variable name unless
this.is explicitly used in the body.
Parameter Variations
Initializing formal parameters support all standard parameter kinds: positional, named, and optional.Positional
The argument is mapped based on its order in the call site.Named
The argument is mapped by key. Named initializing parameters can be markedrequired or provided with default values.
Optional Positional
The argument is mapped by order but is not mandatory.Interaction with Modifiers
finalFields: Because the assignment occurs during the initialization phase (not the body execution phase), initializing formal parameters are valid for initializingfinalfields.- Null Safety:
- If the field is non-nullable, the parameter must be mandatory (positional or
requirednamed) or have a default value. - If the field is nullable, the parameter may be optional without a default value.
- If the field is non-nullable, the parameter must be mandatory (positional or
constConstructors: Initializing formal parameters can be used in constant constructors, provided the assignment results in a compile-time constant state.
Master Dart with Deep Grasping Methodology!Learn More





