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 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.
abstract class BaseClass {
  ReturnType get propertyName;
}

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.
abstract class Shape {
  // Abstract getter
  double get area; 
}

// 1. Satisfied via a concrete getter
class Circle extends Shape {
  final double radius;
  Circle(this.radius);

  @override
  double get area => 3.14159 * radius * radius;
}

// 2. Satisfied via a final field
class Square extends Shape {
  @override
  final double area; 
  
  Square(this.area);
}

// 3. Satisfied via a mutable field
class Rectangle extends Shape {
  @override
  double area; // Implicitly provides 'double get area' and 'set area(double)'
  
  Rectangle(this.area);
}

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.
abstract class Animal {
  Object get identifier;
}

class Dog extends Animal {
  // Covariant return type: 'String' is a subtype of 'Object'
  @override
  String get identifier => "DOG-001"; 
}
Master Dart with Deep Grasping Methodology!Learn More