DeepSource has joined Harness. Learn more
Dart Analyze logoDart Analyze/
DRT-W1408

Constant values from a deferred library can't be used in annotationsDRT-W1408

Major severityMajor
Bug Risk categoryBug Risk

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.