Interview frequency: …
Short answer
late defers the initialization of a non-nullable variable, but reading the variable before it is assigned causes a LateInitializationError at runtime.
Explanation
Use late when you are sure the value will be set before the first read, such as a value computed inside an object's lifecycle. The keyword does not remove the need for initialization; it only moves the check from compile time to runtime, so it is not suitable for hiding an unclear initialization order.
Code example
late final String token;
void configure() {
token = loadToken();
}Common mistakes
- Using late just to silence the compiler without guaranteeing the initialization order.
- Forgetting that a late final cannot be assigned twice.