use_ late_ for_ private_ fields_ and_ variables
Learn about the use_late_for_private_fields_and_variables linter rule.
Use late for private members with a non-nullable type.
Details
#
Use late for private members with non-nullable
types that are always expected to be non-null. Thus it's clear
that the field is not expected to be null
and it avoids null checks.
BAD:
int? _i;
m() {
_i!.abs();
}
GOOD:
late int _i;
m() {
_i.abs();
}
OK:
int? _i;
m() {
_i?.abs();
_i = null;
}
Enable
#
To enable the
use_late_for_private_fields_and_variables rule,
add use_late_for_private_fields_and_variables
under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- use_late_for_private_fields_and_variables
If you're instead using the YAML map syntax to configure
linter rules, add
use_late_for_private_fields_and_variables: true
under linter > rules:
linter:
rules:
use_late_for_private_fields_and_variables: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.