avoid_ returning_ null_ for_ void
Learn about the avoid_returning_null_for_void linter rule.
Avoid returning null for void.
Details
#AVOID returning null for void.
In a large variety of languages void as a return type is
used to indicate that a function doesn't return anything.
Dart allows returning null in functions with a void return type,
but it also allows using return; without specifying any value.
For consistency, use an empty return; instead of returning null.
BAD:
void f1() {
return null;
}
Future<void> f2() async {
return null;
}
GOOD:
void f1() {
return;
}
Future<void> f2() async {
return;
}
Enable
#
To enable the avoid_returning_null_for_void rule, add avoid_returning_null_for_void
under
linter > rules in your analysis_options.yaml
file:
linter:
rules:
- avoid_returning_null_for_void
If you're instead using the YAML map syntax to configure linter rules,
add avoid_returning_null_for_void: true under linter > rules:
linter:
rules:
avoid_returning_null_for_void: true
Unless stated otherwise, the documentation on this site reflects Dart 3.13.3. Report an issue.