function-always-returns-null
preset: recommended
Warns when a function with nullable return type returns only null
.
⚙️ Config
Set ignored-invocations
(default is empty) to ignore function expressions within specific function invocations (example).
analysis_options.yaml
dart_code_metrics:
rules:
- function-always-returns-null:
ignored-invocations:
- useCallback
Example
❌ Bad:
// LINT: This function always returns null. Try checking it for a potential mistake.
String? function() {
if (value == 2) {
// ...
}
return null;
}
✅ Good:
String? function() {
if (value == 2) {
// ...
return 'some string'; // Correct, returns a non-null value in some cases
}
return null;
}
Example with "ignored-invocations"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- function-always-returns-null:
ignored-invocations:
- useCallback
✅ Good:
void function() {
useCallback(() { // Correct, ignored
...
return null;
});
}