prefer_ const_ declarations
Use 'const' for final variables initialized to a constant value.
Description
#
The analyzer produces this diagnostic when a top-level variable, static
field, or local variable is marked as
final
and is initialized to a
constant value.
Examples
#
The following code produces this diagnostic because the top-level variable
v
is both
final
and initialized to a constant value:
final v = const <int>[];
The following code produces this diagnostic because the static field
f
is both
final
and initialized to a constant value:
class C {
static final f = const <int>[];
}
The following code produces this diagnostic because the local variable
v
is both
final
and initialized to a constant value:
void f() {
final v = const <int>[];
print(v);
}
Common fixes
#
Replace the keyword
final
with
const
and remove
const
from the
initializer:
class C {
static const f = <int>[];
}
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.