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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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.
ONE 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.
Read the original → dart.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.