const_initialized_with_non_constant_value_from_deferred_library
Constant values from a deferred library can't be used to initialize a 'const' variable.
Description
#The analyzer produces this diagnostic when a const
variable is
initialized using a const
variable from a library that is imported using
a deferred import. Constants 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 variable pi
is
being initialized using the constant math.pi
from the library
dart:math
, and dart:math
is imported as a deferred library:
import 'dart:math' deferred as math;
const pi = math.pi;
Common fixes
#If you need to reference the value of the constant from the imported
library, then remove the keyword deferred
:
import 'dart:math' as math;
const pi = math.pi;
If you don't need to reference the imported constant, then remove the reference:
const pi = 3.14;
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。