collection_ element_ from_ deferred_ library
Details about the 'collection_element_from_deferred_library' diagnostic produced by the Dart analyzer.
Constant values from a deferred library can't be used as keys in a 'const' map literal.
Constant values from a deferred library can't be used as values in a 'const' constructor.
Constant values from a deferred library can't be used as values in a 'const' list literal.
Constant values from a deferred library can't be used as values in a 'const' map literal.
Constant values from a deferred library can't be used as values in a 'const' set literal.
Description
#
The analyzer produces this diagnostic when a collection
literal that is either explicitly (because it's prefixed by
the const keyword) or implicitly (because it
appears in a
constant contextConstant contextA region of code where the const keyword is implied and
everything within that region must be a constant.
Learn more) a constant contains a value that is declared in 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
#
Given a file a.dart that defines the constant
zero:
const zero = 0;
The following code produces this diagnostic because the
constant list literal contains a.zero, which is
imported using a deferred
import:
import 'a.dart' deferred as a;
var l = const [a.zero];
Common fixes
#
If the collection literal isn't required to be constant, then
remove the
const keyword:
import 'a.dart' deferred as a;
var l = [a.zero];
If the collection is required to be constant and the imported
constant must be referenced, then remove the keyword
deferred from the import:
import 'a.dart' as a;
var l = const [a.zero];
If you don't need to reference the constant, then replace it with a suitable value:
var l = const [0];
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.