non_constant_list_element
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 context).
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.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。