final field in Dart is a single-assignment variable whose value is determined at runtime and cannot be modified once initialized. Unlike const, which requires a compile-time constant, a final variable can be assigned a value based on logic executed when the program runs, but that assignment can occur only once.
Syntax
A final field is declared using thefinal keyword. Type annotation is optional if the type can be inferred.
Initialization Rules
In the context of a class, afinal instance variable must be initialized before the constructor body executes. There are three mechanisms to achieve this:
- Inline Initialization: Assigning the value directly at the point of declaration.
- Initializing Formal: Using the
this.variableNamesyntax in the constructor parameters. - Initializer List: Assigning the value in the constructor’s initializer list (after the
:and before the body).
Reference Immutability vs. Object Mutability
Thefinal keyword enforces referential immutability, not deep immutability.
- Reference: You cannot reassign the variable to point to a different object in memory.
- Object State: If the object referenced by the
finalvariable is mutable (such as aListor a class with non-final fields), its internal state can still be modified.
Late Final Initialization
Thelate modifier can be combined with final to defer initialization. A late final variable:
- Does not require immediate initialization in the constructor or at declaration.
- Must be assigned a value exactly once before it is accessed.
- Throws a runtime exception if assigned a value more than once.
Static Final Fields
When a field is declared asstatic final, it becomes a class-level variable. It is lazily initialized, meaning the initializer expression is not evaluated until the field is accessed for the first time.
Master Dart with Deep Grasping Methodology!Learn More





