跳转至主要内容

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:

dart
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:

dart
import 'dart:math' as math;

class C {
  const C(double d);
}

@C(math.pi)
void f () {}