non_constant_annotation_constructor
Annotation creation can only call a const constructor.
Description
#The analyzer produces this diagnostic when an annotation is the invocation of an existing constructor even though the invoked constructor isn't a const constructor.
Example
#The following code produces this diagnostic because the constructor for C
isn't a const constructor:
@C()
void f() {
}
class C {
C();
}
Common fixes
#If it's valid for the class to have a const constructor, then create a const constructor that can be used for the annotation:
@C()
void f() {
}
class C {
const C();
}
If it isn't valid for the class to have a const constructor, then either remove the annotation or use a different class for the annotation.
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.