Skip to main content
An abstract setter is a property mutator declared within an abstract class or mixin that defines a method signature without providing an implementation body. It establishes a structural contract, compelling concrete subclasses to define the specific logic for value assignment.

Syntax

The declaration uses the set keyword, followed by the property name and a single parameter, terminating with a semicolon rather than a code block.
abstract class EnclosingType {
  // Abstract setter declaration
  set propertyName(Type identifier);
}

Technical Characteristics

  • Signature Definition: It defines the name of the property and the expected data type of the value being assigned.
  • No Implementation: The absence of a method body ({ ... }) signifies that the enclosing class delegates the behavior to its descendants.
  • Parameter Requirement: The setter must accept exactly one parameter.
  • Type Safety: Subclasses implementing the setter must accept a value of the same type (or a supertype) defined in the abstract declaration to satisfy the interface contract.

Concrete Implementation

Any non-abstract class extending the parent class must override the abstract setter. The implementation provides the backing storage or logic required to handle the assignment.
abstract class Device {
  // Abstract setter
  set volume(int level);
}

class Speaker extends Device {
  int _internalVolume = 0;

  @override
  set volume(int level) {
    // Concrete implementation logic
    _internalVolume = level.clamp(0, 100);
  }
}

Relationship to Implicit Interfaces

In Dart, every class implicitly defines an interface. An abstract setter explicitly adds a write-only or read-write requirement to that interface without committing to a specific field storage strategy (such as a private variable) in the parent class.
Master Dart with Deep Grasping Methodology!Learn More