prefer_ const_ declarations
Details about the 'prefer_const_declarations' diagnostic produced by the Dart analyzer.
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>[];
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.