Skip to main content
An abstract getter in Dart is a property accessor declared without a method body within an abstract class, mixin, or interface. It establishes a strict contract that compels any concrete subclass to provide an implementation for retrieving a specific value, deferring the actual computation or state resolution to the implementing type.

Syntax

An abstract getter is defined using the get keyword followed by the property name and a semicolon, omitting the block {} or arrow => syntax.

Implementation Mechanics

When a concrete class inherits or implements an abstract class containing an abstract getter, the Dart compiler requires the subclass to satisfy the getter contract. This can be achieved in three distinct ways:
  1. Concrete Getter: Providing an explicit getter method with a body.
  2. Final Field: Declaring a final instance variable, which implicitly generates a getter without a setter.
  3. Mutable Field: Declaring a standard instance variable, which implicitly generates both a getter (satisfying the contract) and a setter.

Technical Rules and Constraints

  • Context Restriction: Abstract getters can only be declared inside abstract classes or mixin declarations. Attempting to declare an abstract getter in a concrete class will result in a compile-time error.
  • Propagation: If a subclass extends an abstract class but fails to provide a concrete implementation for the abstract getter, the subclass itself must be declared as abstract.
  • Covariance: The implementing class can narrow the return type of the abstract getter to a strict subtype of the originally declared return type.
  • Interface Implementation: If a class implements another class (treating it as an interface), any getters in the parent class are treated as abstract in the context of the implementing class, requiring explicit overrides.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More