Skip to content

avoid_late_final_reassignment

v1.0.0 Warning Resource Management

This rule flags a late final field assigned more than once on the same straight-line path.

This rule is in the core preset.

late final promises one assignment, and Dart enforces it — but at run time, by throwing LateInitializationError on the second write. A second assignment the analyzer can see on one path is therefore a guaranteed crash, not a possibility, and it is worth catching before the code runs.

Only assignments in the same block are compared, without following branches. Two writes in opposite arms of an if are exactly how a late final is meant to be initialised, so they are left alone.

See also: late variables

class Session {
late final String token;
void start(String value) {
token = value;
token = value.trim(); // throws LateInitializationError
}
}
class Session {
late final String token;
void start(String value) {
token = value.trim();
}
}

Initialising through branches is fine:

class Session {
late final String token;
void start(bool isGuest) {
if (isGuest) {
token = 'guest';
} else {
token = generate();
}
}
}

To disable this rule:

many_lints.yaml
rules:
avoid_late_final_reassignment: false

To keep the rule on but skip certain paths, use per-rule exclude.