use_late_for_private_fields_and_variables
Use late for private members with a non-nullable type.
此规则目前处于 experimental 状态,自 Dart 2.10 版本起可用。
详情
#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:
dart
int? _i;
m() {
_i!.abs();
}
GOOD:
dart
late int _i;
m() {
_i.abs();
}
OK:
dart
int? _i;
m() {
_i?.abs();
_i = null;
}
使用方法
#要启用 use_late_for_private_fields_and_variables
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 use_late_for_private_fields_and_variables
:
analysis_options.yaml
yaml
linter:
rules:
- use_late_for_private_fields_and_variables
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.