invalid_annotation_constant_value_from_deferred_library
Constant values from a deferred library can't be used in annotations.
Description
#The analyzer produces this diagnostic when a constant defined in a library that is imported as a deferred library is referenced in the argument list of an annotation. Annotations are evaluated at compile time, and values from deferred libraries aren't available at compile time.
For more information, check out Lazily loading a library.
Example
#The following code produces this diagnostic because the constant pi
is being referenced in the argument list of an annotation, even though the library that defines it is being imported as a deferred library:
import 'dart:math' deferred as math;
class C {
const C(double d);
}
@C(math.pi)
void f () {}
Common fixes
#If you need to reference the imported constant, then remove the deferred
keyword:
import 'dart:math' as math;
class C {
const C(double d);
}
@C(math.pi)
void f () {}
If the import is required to be deferred and there's another constant that is appropriate, then use that constant in place of the constant from the deferred library.
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.