Difference between Const and Final in Dart

Let’s find out the difference between const and final keywords in Dart and Flutter

Jeroen Ouwehand
ITNEXT
Published in
2 min readApr 29, 2020

--

Are const and final in Dart the same?

With my JavaScript/TypeScript background I was already familiar with the const keyword. In Java, you don’t have const, but the equal is final. When I started working with Dart (for Flutter) I was surprised by both of these two keywords available, for me they looked exactly the same. But of course, they are not.

Comparing the keywords

const String personConst = 'Jeroen';
final String personFinal = 'Jeroen';
personConst = 'Bob'; // Not allowed
personFinal = 'Bob'; // Not allowed

In the code above we create a const and a final variable and assign my name to both. You can’t re-assign both of them.

But what is the difference?

final

  • A variable with the final keyword will be initialized at runtime and can only be assigned for a single time.
  • In a class and function, you can define a final variable.
  • For Flutter specific, when the state is updated, everything in the build method will be initialized again. This…

--

--