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.

A static setter in Dart is a class-level mutator method declared with the static and set keywords. It enables write access to static state or encapsulates class-level mutation logic using the standard assignment operator (=). Because it is bound to the class namespace rather than an instantiated object, a static setter executes in a static context and cannot access instance members or the this reference.

Syntax

A static setter is defined inside a class block and must accept exactly one parameter.
class ClassName {
  static set propertyName(Type parameterName) {
    // Class-level mutation logic
  }
}

Invocation

Static setters are invoked directly on the class identifier, not on an instance of the class. The invocation syntax mimics standard variable assignment.
ClassName.propertyName = value;

Technical Characteristics

  • Single Parameter Requirement: A static setter must define exactly one required positional parameter. It cannot accept optional, named, or multiple parameters.
  • Return Type Constraints: Setters in Dart inherently return void. While the return type is typically omitted in practice, explicitly declaring void (e.g., static void set propertyName(...)) is perfectly valid. However, declaring any return type other than void results in a compile-time error.
  • Static Context Isolation: The execution scope of a static setter is strictly limited to other static members (variables, getters, setters, and methods) within the same class. Attempting to reference this or any instance-level member will cause a compilation error.
  • Namespace Resolution: A static setter shares the class namespace with static getters. You can define a static getter and a static setter with the same name to create a read-write property, but you cannot share the name with a static method or a static field.

Code Example

class SystemConfig {
  // Private static backing field
  static int _maxConnections = 10;

  // Static setter declaration (explicit void is optional but valid)
  static void set maxConnections(int value) {
    // Mutation logic operating strictly on static state
    _maxConnections = value.clamp(1, 100);
  }
  
  static int get maxConnections => _maxConnections;
}

void main() {
  // Invoking the static setter via assignment
  SystemConfig.maxConnections = 150; 
  
  print(SystemConfig.maxConnections); // Outputs: 100
}
Master Dart with Deep Grasping Methodology!Learn More