invalid_annotation
Annotation must be either a const variable reference or const constructor invocation.
Description
#The analyzer produces this diagnostic when an annotation is found that is using something that is neither a variable marked as const
or the invocation of a const
constructor.
Getters can't be used as annotations.
Examples
#The following code produces this diagnostic because the variable v
isn't a const
variable:
var v = 0;
@v
void f() {
}
The following code produces this diagnostic because f
isn't a variable:
@f
void f() {
}
The following code produces this diagnostic because f
isn't a constructor:
@f()
void f() {
}
The following code produces this diagnostic because g
is a getter:
@g
int get g => 0;
Common fixes
#If the annotation is referencing a variable that isn't a const
constructor, add the keyword const
to the variable's declaration:
const v = 0;
@v
void f() {
}
If the annotation isn't referencing a variable, then remove it:
int v = 0;
void f() {
}
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.