invalid_ annotation
Details about the 'invalid_annotation' diagnostic produced by the Dart analyzer.
Annotation must be either a const variable reference or const constructor invocation.
Description
#
The analyzer produces this diagnostic when an annotation without
arguments references something that is neither a variable marked as
const nor a constructor. An annotation with arguments, such as @f(),
is an invocation of a const constructor, so when f isn't a class it's
reported like any other constant constructor invocation, as
creation_with_non_type.
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 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.13.3. Report an issue.