referenced_before_declaration
Local variable '{0}' can't be referenced before it is declared.
Description
#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);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.