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 setter in Dart is a mutator method declared without an implementation body within an abstract class. It establishes a strict structural contract, compelling any concrete subclass to provide the implementation logic for assigning a value to a specific property identifier.

Syntax

An abstract setter is defined using the set keyword followed by the property name and a single parameter, terminated by a semicolon rather than a method body.
abstract class AbstractClass {
  set propertyName(Type value);
}

Implementation Mechanics

When a concrete class inherits or implements an abstract class containing an abstract setter, it must satisfy the contract. Dart allows this contract to be fulfilled in two distinct ways:
  1. Explicit Setter: Defining a concrete mutator method using the set keyword.
  2. Implicit Setter: Declaring a non-final, mutable instance variable of the exact same name and type. Dart automatically generates an implicit setter for mutable fields, which satisfies the abstract contract.
abstract class Configuration {
  // Abstract setter declaration
  set threshold(double value);
}

// Fulfillment via Explicit Setter
class StrictConfiguration extends Configuration {
  double _internalThreshold = 0.0;

  @override
  set threshold(double value) {
    _internalThreshold = value;
  }
}

// Fulfillment via Implicit Setter (Mutable Field)
class DefaultConfiguration extends Configuration {
  @override
  double threshold = 10.5; 
}

Technical Constraints and Rules

  • Parameter Type Compatibility (Contravariance): The parameter type of the overriding setter must be the same type or a supertype of the parameter type defined in the abstract setter. Dart allows widening the parameter type (e.g., overriding set threshold(double value) with set threshold(num value)).
  • Covariance: If a subclass needs to narrow the parameter type (restricting the setter to accept only a subtype), the covariant keyword must be applied to the parameter in either the abstract declaration or the concrete implementation to bypass static type checking errors.
  • Return Type: Setters in Dart inherently return void. While explicitly declaring void before the set keyword is syntactically valid (void set propertyName(Type value);), it is redundant and generally omitted in idiomatic Dart.
  • Independence: An abstract setter is an independent method signature. It does not require a corresponding abstract getter, though they are frequently declared together to define a fully mutable abstract property.
Master Dart with Deep Grasping Methodology!Learn More