avoid_returning_null_for_void
Avoid returning null
for void
.
此规则自 Dart 2.1 版本起可用。
_规则集:recommended , flutter _
此规则提供 快速修复 。
详情
#AVOID returning null
for void
.
In a large variety of languages void
as return type is used to indicate that a function doesn't return anything. Dart allows returning null
in functions with void
return type but it also allow using return;
without specifying any value. To have a consistent way you should not return null
and only use an empty return.
BAD:
dart
void f1() {
return null;
}
Future<void> f2() async {
return null;
}
GOOD:
dart
void f1() {
return;
}
Future<void> f2() async {
return;
}
使用方法
#要启用 avoid_returning_null_for_void
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 avoid_returning_null_for_void
:
analysis_options.yaml
yaml
linter:
rules:
- avoid_returning_null_for_void
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.