Skip to main content
A constant field in Dart is a class member variable declared with the static const modifiers. It represents a value that is fully resolved and frozen at compile-time, resulting in a single, immutable value shared across all instances of the class.

Syntax

To declare a constant field within a class, the static modifier is mandatory alongside const.
class Configuration {
  // Explicit typing
  static const int maxRetries = 5;

  // Type inference
  static const defaultProtocol = 'https';
  
  // Complex constant value (Constructor must be const)
  // Duration is a standard class in dart:core
  static const Duration timeout = Duration(seconds: 30);
}

Technical Characteristics

1. Static Requirement

In Dart, a const variable defined inside a class must be marked static. A compile-time constant exists independently of any class instance; its memory is allocated when the class is loaded, not when an object is instantiated. Consequently, instance variables cannot be declared as const.

2. Compile-Time Resolution

The value assigned to a constant field must be a constant expression. The compiler must be able to calculate the value without executing runtime code. Valid assignments include:
  • Literals (numbers, strings, booleans).
  • References to other compile-time constants.
  • Arithmetic or logical operations on constant values.
  • Instances of classes created via const constructors.

3. Transitive Immutability

Constant fields are deeply immutable. When a constant field holds a reference to an object, the object and its entire reachable state are frozen.
  • Class Instances: Fields within a class instantiated with const must be final. Attempting to reassign a field results in a compile-time error.
  • Collections: Standard collections (like List or Map) defined as const are converted to unmodifiable views. Attempting to call mutation methods (e.g., .add or .clear) results in a runtime exception (UnsupportedError).
class Constants {
  // The list reference and its contents are frozen
  static const List<int> fibonacci = [1, 1, 2, 3, 5];
}

void main() {
  // Runtime Error: Unsupported operation: Cannot add to an unmodifiable list
  Constants.fibonacci.add(8); 
}

4. Canonicalization

Dart canonicalizes constant values. If multiple constant fields—even across different classes or libraries—define identical values or identical object structures using const constructors, they refer to the exact same memory location.
import 'dart:math';

class A {
  static const point = Point(1, 1);
}

class B {
  static const point = Point(1, 1);
}

void main() {
  // Returns true; both fields reference the same memory address
  print(identical(A.point, B.point)); 
}

Distinction from Final Fields

While both final and const fields are immutable after initialization, they differ in initialization timing and scope:
FeatureConstant Field (static const)Final Field (final)
Resolution TimeCompile-timeRuntime
ScopeClass-level (Static)Instance-level (usually)
MemoryCanonicalized (deduplicated)Unique per instance
Value SourceMust be a constant expressionCan be a runtime calculation or function result
Master Dart with Deep Grasping Methodology!Learn More