unnecessary_final
Don't use final
for local variables.
This rule is available as of Dart 2.7.
This rule has a quick fix available.
Incompatible rules: prefer_final_locals, prefer_final_parameters, prefer_final_in_for_each
Details
#Use var
, not final
, when declaring local variables.
Per Effective Dart,
there are two styles in wide use. This rule enforces the var
style.
For the alternative style that prefers final
, enable prefer_final_locals
and prefer_final_in_for_each
instead.
For fields, final
is always recommended; see the rule prefer_final_fields
.
BAD:
void badMethod() {
final label = 'Final or var?';
for (final char in ['v', 'a', 'r']) {
print(char);
}
}
GOOD:
void goodMethod() {
var label = 'Final or var?';
for (var char in ['v', 'a', 'r']) {
print(char);
}
}
Usage
#To enable the unnecessary_final
rule,
add unnecessary_final
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_final
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。