Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An overridden getter in Dart is a property accessor in a subclass that redefines the retrieval logic of a property inherited from its superclass. Because Dart adheres to the Uniform Access Principle by implicitly generating getters for all instance variables, a subclass can override both explicitly defined superclass getters and standard instance fields. To override a getter, the subclass uses the @override annotation followed by the get keyword, ensuring the signature conforms to the parent’s contract.

Overriding an Explicit Getter

When a superclass defines a property using the get keyword, the subclass can replace its implementation.
class BaseClass {
  String get identifier => 'Base-001';
}

class SubClass extends BaseClass {
  @override
  String get identifier => 'Sub-001';
}

Overriding an Instance Field with a Getter

Because a standard field in Dart implicitly exposes a getter, a subclass can override a stored variable from the superclass and replace it with a computed property.
class BaseClass {
  final double threshold = 10.5;
}

class SubClass extends BaseClass {
  @override
  double get threshold => super.threshold * 2.0;
}

Technical Constraints and Rules

  • Covariant Return Types: The return type of the overridden getter must be the same as, or a subtype of, the return type of the superclass getter. You cannot widen the return type.
class BaseClass {
  num get value => 10;
}

class SubClass extends BaseClass {
  @override
  int get value => 20; // Valid: int is a subtype of num
}
  • Super Invocation: The subclass getter can access the superclass implementation or field value using the super keyword (super.propertyName).
  • Setter Inheritance: If the superclass defines a mutable (non-final) field, it implicitly creates both a getter and a setter. Overriding only the getter in the subclass modifies the read behavior, but the implicit setter inherited from the superclass remains intact and functional unless explicitly overridden as well.
  • Annotation Requirement: While the @override annotation is technically optional in Dart, it is strictly enforced by the Dart analyzer in standard linting rules to prevent silent failures if the superclass contract changes.
Master Dart with Deep Grasping Methodology!Learn More