collection_ element_ from_ deferred_ library
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 context) 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];
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.