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, thestatic modifier is mandatory alongside const.
Technical Characteristics
1. Static Requirement
In Dart, aconst 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
constconstructors.
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
constmust befinal. Attempting to reassign a field results in a compile-time error. - Collections: Standard collections (like
ListorMap) defined asconstare converted to unmodifiable views. Attempting to call mutation methods (e.g.,.addor.clear) results in a runtime exception (UnsupportedError).
4. Canonicalization
Dart canonicalizes constant values. If multiple constant fields—even across different classes or libraries—define identical values or identical object structures usingconst constructors, they refer to the exact same memory location.
Distinction from Final Fields
While bothfinal and const fields are immutable after initialization, they differ in initialization timing and scope:
| Feature | Constant Field (static const) | Final Field (final) |
|---|---|---|
| Resolution Time | Compile-time | Runtime |
| Scope | Class-level (Static) | Instance-level (usually) |
| Memory | Canonicalized (deduplicated) | Unique per instance |
| Value Source | Must be a constant expression | Can be a runtime calculation or function result |
Master Dart with Deep Grasping Methodology!Learn More





