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>[];
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。