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];
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.