Difference between final and const Dart class properties?
This tests your grasp of Dart's compile-time versus runtime constant model. A strong answer contrasts final variables set once with static const compile-time values, notes const is implicitly final, gives examples. Red flag: const as non-static instance field.
WHAT THIS TESTS: Whether you understand the distinction between runtime single-assignment and compile-time constants in Dart, and how that distinction shapes class design, memory layout, and API contracts. At a senior level, the interviewer wants to see that you know when immutability is enforced, why const objects are canonicalized by the compiler, and how choosing between final and const impacts widget rebuilds and object identity in Flutter applications. It also reveals whether you understand that final only freezes the reference, not the underlying object state.
A GOOD ANSWER COVERS: First, explain that final means the variable's reference can be assigned exactly once and that assignment occurs at runtime, so values like DateTime.now() or a parsed JSON string can be final but never const. Second, state that const means the value is a compile-time constant and that const variables are implicitly final. Third, clarify that inside a class, const fields must be static because instance fields are determined per object at runtime, whereas final is allowed as an instance field. Fourth, mention that const objects are canonicalized, meaning two identical const values point to the same memory location, which reduces allocations and improves equality checks. Fifth, provide a class-level example that demonstrates both concepts side by side.
COMMON WRONG ANSWERS: Claiming that final and const are interchangeable or that both simply mean immutable. Saying const can be declared as a non-static instance property. Asserting that final makes the entire object deeply immutable rather than just freezing the reference. Forgetting that const collections such as lists or maps require their elements to also be compile-time constants. Ignoring that const constructors can only assign to const fields and that every field in the object must be final or const. Stating that const values can be initialized from method return values or user input.
LIKELY FOLLOW-UPS: How does const canonicalization affect performance and widget tree rebuilding in Flutter? Can you create a const list that contains final objects? What is the difference between a const variable and a const constructor? When should you use a const constructor versus a final field for a data model? What happens if you try to assign a const value to a final variable, and does the reverse work? How do late final variables interact with const semantics?
ONE CONCRETE EXAMPLE: Consider a class named ApiConfig. Inside it, you write static const String baseUrl = 'https://api.example.com' because the URL is known when the code compiles and should never change. You also declare final String apiKey and assign it through the constructor because the key is only available at runtime after reading environment variables or user input. This design keeps shared configuration canonicalized and memory-efficient while still allowing runtime-specific values to be bound once and then protected from mutation.
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.