Skip to main content
A static getter in Dart is a class-level accessor method defined using the static and get keywords. It allows read-only access to a computed or encapsulated value directly through the class identifier, rather than through an instantiated object. Because it is bound to the class’s namespace and executes in a static context, a static getter cannot access instance members or the this reference.

Syntax

A static getter is declared inside a class using the static modifier followed by the return type, the get keyword, and the identifier. It can be written using either a block body or an arrow (=>) expression.

Technical Characteristics

  • Invocation: Static getters are invoked directly on the class name without parentheses. The syntax is identical to accessing a static variable (ClassName.propertyName).
  • Scope Restrictions: The execution context of a static getter is strictly limited to the class level. Attempting to reference this or any non-static variables/methods within the getter’s body will result in a compile-time error.
  • Accessing Internal State: Static getters are frequently used to expose private static variables (denoted by a leading underscore _).
  • Read-Only Nature: Defining a static getter without a corresponding static setter makes the property read-only to the caller. The underlying value can only be mutated if the class provides a separate mechanism to modify the internal static state.

Code Example

Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More