Dart Analyze logoDart Analyze/
DRT-W1595

Variable referenced before declarationDRT-W1595

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a variable is referenced before it's declared. In Dart, variables are visible everywhere in the block in which they are declared, but can only be referenced after they are declared.

The analyzer also produces a context message that indicates where the declaration is located.

Example

The following code produces this diagnostic because i is used before it is declared:

void f() {
  print(i);
  int i = 5;
}

Common fixes

If you intended to reference the local variable, move the declaration before the first reference:

void f() {
  int i = 5;
  print(i);
}

If you intended to reference a name from an outer scope, such as a parameter, instance field or top-level variable, then rename the local declaration so that it doesn't hide the outer variable.

void f(int i) {
  print(i);
  int x = 5;
  print(x);
}