set) originally declared in a superclass. This enables the subclass to intercept assignment operations, allowing for the execution of custom logic, validation, or state management before, after, or instead of the superclass’s implementation.
Syntax and Implementation
To override a setter, the subclass must define aset accessor matching the name of the property in the superclass. The @override annotation enforces compile-time verification that the member exists in the superclass.
When overriding an explicit setter (one defined with the set keyword in the parent), the subclass implementation replaces the parent’s method body.
Overriding Implicit Setters (Fields)
In Dart, a non-final instance variable (field) implicitly generates both a getter and a setter. To override the setter logic of a field declared in a superclass, the subclass must explicitly override both the getter and the setter. Overriding only the setter is a compile-time error, as it would result in an incomplete property definition in the subclass. The subclass typically usessuper to delegate storage and retrieval to the superclass’s field.
Type Constraints and Covariance
By default, the parameter type of an overridden setter must be the same as, or a supertype of, the parameter type defined in the superclass. This adheres to standard contravariance rules for function parameters. To narrow the parameter type (accept a subtype) in the subclass setter, thecovariant keyword is required. This disables static type safety checks for that specific parameter, deferring type verification to runtime.
Execution Flow and State Management
- Invocation: When an assignment is made to the property on a subclass instance, the runtime invokes the subclass’s
setimplementation. - Interception: The code block within the subclass setter executes.
- Delegation: If the subclass does not maintain its own state for the property, it must invoke
super.propertyName = value.- If the superclass defined an explicit setter, this calls that method.
- If the superclass defined a field, this writes directly to that storage slot.
- Omitting the
supercall prevents the value from propagating to the parent class, potentially leaving the object state unchanged regarding that property.
Master Dart with Deep Grasping Methodology!Learn More





