non_ constant_ list_ element
Details about the 'non_constant_list_element' diagnostic produced by the Dart analyzer.
The values in a const list literal must be constants.
Description
#
The analyzer produces this diagnostic when an element in a
constant list literal isn't a constant value. The list literal
can be constant 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).
Example
#
The following code produces this diagnostic because
x isn't a constant, even though it appears in an
implicitly constant list literal:
var x = 2;
var y = const <int>[0, 1, x];
Common fixes
#
If the list needs to be a constant list, then convert the
element to be a constant. In the example above, you might add
the const keyword to the declaration of
x:
const x = 2;
var y = const <int>[0, 1, x];
If the expression can't be made a constant, then the list
can't be a constant either, so you must change the code so
that the list isn't a constant. In the example above this
means removing the const keyword before the list
literal:
var x = 2;
var y = <int>[0, 1, x];
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.