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 constant field in Dart is a class-level variable whose value is deeply immutable and evaluated entirely at compile time. By combining the static and const modifiers, the field is bound to the class namespace rather than any specific instance, and its memory is allocated and canonicalized only once during the compilation phase.

Syntax

class ClassName {
  static const Type fieldName = compileTimeExpression;
}

Technical Characteristics

  • Compile-Time Evaluation: The right-hand side of the assignment must be a valid constant expression. This includes literals, arithmetic operations on other constants, or invocations of const constructors. It cannot rely on runtime execution (e.g., DateTime.now() or method returns).
  • Class-Bound Access: Because the field is static, it is accessed strictly through the class name (ClassName.fieldName). Attempting to access it through an instance of the class will result in a compilation error.
  • Deep Immutability: The const modifier enforces transitive immutability. If the static constant is a collection (such as a List, Set, or Map) or an object instantiated via a const constructor, all nested elements and fields are permanently frozen.
  • Canonicalization: Dart’s compiler optimizes const values. If multiple static constants or variables across the application evaluate to the identical compile-time state, they point to the exact same memory reference.
  • Mandatory Static Modifier: In Dart, an instance field cannot be const. Any field declared as const within a class must explicitly include the static modifier.
  • Inline Initialization: A static constant must be initialized at the point of declaration. It cannot be initialized later in a constructor, an initializer list, or a static block.

Code Demonstration

class SystemMetrics {
  // Primitive static constant
  static const int maxThreads = 16;

  // Computed static constant (valid because operands are constants)
  static const int threadPoolLimit = maxThreads * 2;

  // Collection static constant (deeply immutable)
  static const List<String> coreModules = ['auth', 'database', 'network'];
  
  // Object static constant (requires a const constructor)
  static const Point origin = Point(0, 0);
}

class Point {
  final int x;
  final int y;
  
  // Const constructor required for static const instantiation
  const Point(this.x, this.y); 
}

void main() {
  // Valid access via class name
  print(SystemMetrics.maxThreads);
  
  // Invalid: SystemMetrics().maxThreads (Cannot access static member via instance)
  // Invalid: SystemMetrics.maxThreads = 32 (Cannot reassign a const variable)
  // Invalid: SystemMetrics.coreModules.add('ui') (Cannot mutate a const collection)
}

Type Inference

While the type can be explicitly declared, Dart’s type inference allows the omission of the type annotation if it can be statically resolved from the assigned value.
class Config {
  // Type explicitly defined
  static const double timeout = 5.5; 
  
  // Type inferred as double
  static const retryDelay = 2.0; 
}
Master Dart with Deep Grasping Methodology!Learn More