Dart DISCUSSION

Dart const versus final: what is the actual difference and when does it matter in Flutter?

Started by edil Dart const vs finalcompile-time constantsconst constructorsimmutabilityFlutter const widgets
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Dart const versus final: what is the actual difference and when does it matter in Flutter?

edil Dart Forum
#1

Both final and const stop me from reassigning a variable in Dart, and the analyzer keeps suggesting that I add const in front of widget constructors. But const now = DateTime.now() is rejected while final now = DateTime.now() is fine, and a final list can still have items added.

What is the precise difference between the two, and is the const the linter asks for only style or does it change how the app behaves?

Community replies 4

Re: Dart const versus final: what is the actual difference and when does it matter in Flutter?

#2

final means the variable is assigned exactly once; the value can be computed at runtime. const means the value is a compile-time constant: it must be fully known when the program is compiled, which is why DateTime.now() cannot be const. Every const is implicitly final, not the other way round.

Typical use: final reading = await sensor.read() for something obtained while running, const maxChannels = 8 for something fixed in the source. Inside a class, an instance field can be final but a constant has to be static const.

Re: Dart const versus final: what is the actual difference and when does it matter in Flutter?

#3

The second difference is depth. final protects only the variable: final list = [1, 2] followed by list.add(3) is fine, since the list object itself is mutable. A const value is deeply immutable: with const list = [1, 2], calling list.add(3) compiles but throws an UnsupportedError at runtime, because the list is unmodifiable.

For a collection that must not change, const gives the stronger guarantee; for one built at runtime, wrap it with List.unmodifiable.

Re: Dart const versus final: what is the actual difference and when does it matter in Flutter?

#4

Const objects are canonicalised: two const expressions with the same value are the same object. identical(const [1, 2], const [1, 2]) is true, while the same comparison with non-const lists is false. A class can take part in this if it has a const constructor, which requires all its fields to be final.

This is why the linter's advice is more than style in Flutter. A widget created with const Text('Ready') is one shared instance. When the parent rebuilds and returns the identical child instance, the framework can skip rebuilding that subtree, and no new object is allocated on each build.

Re: Dart const versus final: what is the actual difference and when does it matter in Flutter?

#5

Some practical details. const on a constructor call applies to everything inside it, so const Padding(padding: EdgeInsets.all(8), child: Text('Ready')) needs the keyword only once. If any argument is a runtime value, such as Text(name), that widget cannot be const, and neither can a parent that contains it, but its constant siblings still can.

A const constructor does not force const usage: the same class can be created with const where the arguments are constants and without it where they are not. Only the const calls are canonicalised.

TEP COMMUNITY