---
title: "Choosing Between final and const for Immutable Values in Dart"
date: "2026-12-03T09:52:00+07:00"
description: "final can be set once at runtime; const must be fixed at compile time. Learn the rules, const canonicalization, and when the const keyword is redundant."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-final-vs-const.jpg"
alt: "Conceptual comparison of the Dart final and const keywords guarding two immutable value boxes"
tags: ['dart', 'dart-language', 'immutability', 'compile-time-constants', 'code-style']
---

Both `final` and `const` mean "you can't reassign this," and because of that they get used almost interchangeably — but they answer different questions. `final` says the variable is set once at runtime; `const` says the value was worked out before the program even started, and that distinction changes what you may write, where you may write it, and what the runtime guarantees.

## final: set once, decided at runtime

A `final` variable can be assigned only once, but *when* that one assignment happens is entirely up to you. Its initializer can call functions, read the clock, or build a fresh object — the constraint is only that nothing may reassign the variable afterward.

```dart
void main() {
  final name = 'Bob';             // Type inferred: String.
  final String nickname = 'Bobby'; // Type annotated, also fine.
  final timestamp = DateTime.now(); // Decided at runtime, assigned once.

  // Error: a final variable can only be set once.
  name = 'Alice';

  print('$name ($nickname) at $timestamp');
}
```

Because `final` only freezes the *reference*, the object it points at can still change internally. `final` labels added to a list stay final while the list itself grows — an important nuance we come back to below. For fields and top-level variables this is also the style default: Effective Dart's `prefer_final_fields` rule says to mark fields and top-level variables `final` whenever they don't need to change, and it pairs naturally with the `var` guidance in `dart-var-inference.md`.

## const: fixed before the program runs

A `const` variable is a compile-time constant. Its initializer must be an expression the compiler can evaluate on its own: literals, other constants, arithmetic on constant numbers, and `const` constructor calls. Nothing that reads the clock, the environment, or any other runtime state qualifies.

```dart
const bar = 1000000; // Unit of pressure (dynes/cm2).
const double atm = 1.01325 * bar; // Standard atmosphere.
const msPerSecond = 1000;
const secondsUntilRetry = 5;
const msUntilRetry = secondsUntilRetry * msPerSecond;

void main() {
  print('$msUntilRetry ms until retry ($atm atm).');
}
```

Try to initialize one with `DateTime.now()` and the compiler objects immediately, because that value cannot be known until the program runs. Constants are also lazily initialized at the top level and in classes, and a `const` declaration at class level must be marked `static const` — instance variables can be `final`, but never `const`.

```dart
class Session {
  static const retryLimit = 3; // OK: static and const.
  final String id;             // OK: instance variables can be final.

  Session(this.id);
}

void main() {
  print('${Session.retryLimit} retries allowed.');
}
```

## const is implicitly final, and the keyword is often redundant

Every `const` variable is automatically `final`, which produces three equivalent-looking declarations that differ in subtle ways:

```dart
void main() {
  var foo = const []; // Mutable reference, const value today.
  final bar = const []; // Set-once reference to a const value.
  const baz = [];      // Equivalent to const [] — const is implied.

  foo = [1, 2, 3]; // Fine: foo is neither final nor const.
  print('$foo $bar $baz');
}
```

You can write `const baz = []` because once a declaration is `const`, its initializer is already in a constant context. That is exactly what the `unnecessary_const` lint polishes off: inside a const collection literal, a const constructor call, a metadata annotation, a const variable's initializer, or a switch case expression, the `const` keyword is implicit, doesn't need to be written, and shouldn't be.

```dart
class Color {
  final String name;
  final List<int> channels;
  const Color(this.name, this.channels);
}

void main() {
  const primaryColors = [
    Color('red', [255, 0, 0]),
    Color('green', [0, 255, 0]),
    Color('blue', [0, 0, 255]),
  ];

  print(primaryColors.first.name);
}
```

Writing `const Color('red', const [255, 0, 0])` there would compile — and then be flagged as redundant, twice. The rule of thumb: any place where writing `new` instead of `const` would be an error is a place where `const` may be omitted.

## Final objects can still change; const objects cannot

This is the sharpest practical difference. A `final` reference cannot be reassigned, but the object it references may be perfectly mutable. A `const` object and its fields are deeply immutable — nothing inside can change, ever.

```dart
void main() {
  final mutable = {'red': 3};
  mutable['blue'] = 1; // OK: only the reference is frozen.

  const frozen = {'red': 3};
  // Error: const maps can't be modified.
  frozen['blue'] = 1;
}
```

The same applies to fields. A `final` object's fields can still be written if they themselves aren't final; a `const` object's fields cannot be changed at all. That deep immutability is what makes `const` objects shareable across the program.

## Canonicalization: identical const objects are the same object

Because `const` values are computed at compile time, the compiler stores one canonical copy of each distinct constant. Two separately written but equal const literals are literally the same object, while two identical-looking runtime lists are not.

```dart
void main() {
  const a = [1, 2, 3];
  const b = [1, 2, 3];

  print(identical(a, b)); // true — both are the same canonical object.
  print(identical([1, 2, 3], [1, 2, 3])); // false — runtime lists differ.
}
```

That identity guarantee is a real optimization — widgets, configuration records, and error tables built as constants can be shared instead of duplicated — and it is only available to `const`. A `final` list created at runtime gets no such sharing.

## Which one should you reach for?

| Question | `var` | `final` | `const` |
|---|---|---|---|
| Reassignable after initialization | Yes | No | No |
| Value decided | Any time | Once, at runtime | Compile time only |
| Initializer must be a constant expression | No | No | Yes |
| Allowed on instance fields | Yes | Yes | No (static members only) |
| Object contents can change | Yes | Yes | No — deeply immutable |
| Canonicalized with `identical()` | No | No | Yes |
| Typical use | Evolving locals, counters | Set-once references and fields | Truly fixed configuration |

The working heuristic is simple. If the value must be known before the program starts — a retry limit, a URL, a palette — use `const`; you get compile-time checking, deep immutability, and canonicalization for free. If the value is computed once during startup or during a function call — a timestamp, a parsed result, a fresh list — use `final`. Everything else stays `var`, as covered in `dart-var-inference.md`. And since number literals and arithmetic on constants are what make `const` initializers possible, the details live in `dart-num-int-double-conversion.md`.

## Takeaways

`final` and `const` both prevent reassignment, but `final` freezes a reference after a single runtime assignment while `const` demands a compile-time constant expression and delivers deep immutability plus canonicalized, shareable objects. Const is implicitly final, instance fields may be final but never const, and inside any constant context the `const` keyword should be omitted rather than repeated.

**Takeaway:** Use `const` when the value is fixed before the program runs, `final` when it's assigned exactly once at runtime, and remember that only `const` makes the object's contents immutable.

## References

- https://dart.dev/language/variables
- https://dart.dev/effective-dart/usage
- https://dart.dev/effective-dart/design
- https://dart.dev/language/built-in-types
