Dart Analyze logoDart Analyze/
DRT-W1580

Final variable can't be read because it's potentially unassigned at this pointDRT-W1580

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a final local variable that isn't initialized at the declaration site is read at a point where the compiler can't prove that the variable is always initialized before it's referenced.

Example

The following code produces this diagnostic because the final local variable x is read (on line 3) when it's possible that it hasn't yet been initialized:

int f() {
  final int x;
  return x;
}

Common fixes

Ensure that the variable has been initialized before it's read:

int f(bool b) {
  final int x;
  if (b) {
    x = 0;
  } else {
    x = 1;
  }
  return x;
}