An overridden setter in Dart is a mutator method within a subclass that redefines the assignment behavior of a property inherited from a superclass. By utilizing theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@override annotation alongside the set keyword, the subclass intercepts write operations directed at the property, allowing for modified assignment logic, type contravariance (or covariance via the covariant keyword), or delegation to the parent implementation via the super keyword.
Syntax and Implementation
To override a setter, the subclass must declare a method using theset keyword with an identifier identical to the superclass setter.
Technical Constraints and Rules
- Signature Matching (Contravariance and Covariance):
By default, the parameter type of the overriding setter must be the same as, or a supertype of, the parameter type in the overridden setter. Dart’s sound type system enforces this contravariance to ensure Liskov Substitution Principle (LSP) compliance. However, Dart allows you to intentionally narrow the parameter type (covariance) by using the
covariantkeyword. If the parameter in the subclass is markedcovariant(e.g.,set configurationValue(covariant int value)), the compiler permits the narrower type, deferring the type check to runtime. - Return Type Declarations:
Setters in Dart inherently do not return a value. While it is syntactically valid to explicitly declare a
voidreturn type in the setter signature (e.g.,void set configurationValue(num value)), the standard Dart linter ruleavoid_return_types_on_settersstrongly discourages this. The idiomatic approach is to omit the return type entirely. - The
@overrideAnnotation: While Dart allows overriding without the@overrideannotation, its use is strictly enforced by standard linting rules. It directs the analyzer to verify that a corresponding setter actually exists in the superclass hierarchy, preventing accidental shadowing if the superclass signature changes. - Field to Setter Promotion: In Dart, a standard instance variable implicitly generates a getter and a setter. Therefore, a subclass can override a standard, non-final field from a superclass by explicitly defining a setter. When doing so, the subclass must also override the corresponding getter. If only the setter is overridden, writing to the property updates the subclass’s state, but reading from the property returns the inherited superclass field’s value, leading to disjointed state.
Master Dart with Deep Grasping Methodology!Learn More





