What is the difference between final and const in Dart?
Tests compile-time versus runtime immutability in Dart. A strong answer: final allows single assignment at runtime, but const requires a compile-time constant and deep immutability.
What's really being asked
This question tests whether you understand Dart's variable declaration modifiers beyond surface-level syntax. Specifically, it checks if you know the difference between single-assignment variables and compile-time constants, and whether you understand when evaluation happens in the compilation pipeline. Senior interviewers care about this because misusing const and final leads to unnecessary widget rebuilds in Flutter, missed canonicalization opportunities, or runtime errors when compile-time guarantees are actually required.
The full answer
A good answer hits four things in order. First, state that a final variable can only be assigned once but its value is determined at runtime, so it can hold the result of a function call or user input. Second, state that a const variable must be initialized with a value known at compile time and that the resulting object is deeply immutable, meaning none of its fields can change. Third, note that const is implicitly final, meaning every const variable is also final, but the reverse is not true. Fourth, explain that const objects are canonicalized, so identical const expressions yield the same object in memory, which matters for performance and equality checks. For the requested code example, use something determined at runtime, such as final currentTime = DateTime.now();, because that method cannot be evaluated during compilation.
The mistakes people make
Common wrong answers include saying both modifiers are simply immutable without explaining the compile-time requirement. Another red flag is claiming that final prevents mutation of the object's internal state; final only prevents reassignment of the variable itself, so a final list can still have items added unless the list itself is const. Some candidates incorrectly say const is only for primitives or confuse const variables with the const constructor context used in Flutter widget trees.
What usually comes next
Interviewers often follow up by asking when to prefer const over final in Flutter widget trees to avoid unnecessary repaints. They may also ask about the const constructor context, such as why child widgets are prefixed with const, or how const affects object identity and the equality operator. Another follow-up is asking what happens if you assign a const list to a final variable and then attempt to modify the list, or how const impacts memory usage in large applications.
A concrete example
A concrete example is final currentTime = DateTime.now();. This works because DateTime.now() produces a value at runtime that is unknowable at compile time. You cannot write const currentTime = DateTime.now(); because the compiler does not know the current time when compiling the program. Another valid example is final randomValue = Random().nextInt(10);, which also requires runtime execution and therefore cannot be const.
Interview question
Which statement accurately describes a key difference between final and const in Dart?
- a.A final variable can hold a runtime value like DateTime.now(), while const requires a compile-time constant.Correct
- b.final variables must be initialized with compile-time constants.
- c.const variables can be initialized with runtime values like DateTime.now().
- d.final prevents all internal mutation of the object it references.
Why? this is the answer
final allows single assignment at runtime, so DateTime.now() is valid, whereas const requires a compile-time constant and deep immutability. Option D is wrong because final only prevents reassignment of the variable, not mutation of the object's contents.
Just read this? Test yourself on what you have been reading.
Read the original → dart.dev
- #dart
- #flutter
- #variables
- #final
- #const
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.
See open roles