null_ closures
Do not pass null as an argument where a closure is expected.
Details
#DON'T pass null as an argument where a closure is expected.
Often a closure that is passed to a method will only be called conditionally,
so that tests and "happy path" production calls do not reveal that null
will
result in an exception being thrown.
This rule only catches null literals being passed where closures are expected in the following locations:
Constructors
#-
From
dart:asyncFutureat the 0th positional parameterFuture.microtaskat the 0th positional parameterFuture.syncat the 0th positional parameterTimerat the 0th positional parameterTimer.periodicat the 1st positional parameter
-
From
dart:coreList.generateat the 1st positional parameter
Static functions
#-
From
dart:asyncscheduleMicrotaskat the 0th positional parameterFuture.doWhileat the 0th positional parameterFuture.forEachat the 0th positional parameterFuture.waitat the named parametercleanupTimer.runat the 0th positional parameter
Instance methods
#-
From
dart:asyncFuture.thenat the 0th positional parameterFuture.completeat the 0th positional parameter
-
From
dart:collectionQueue.removeWhereat the 0th positional parameter- `Queue.retain
Iterable.firstWhereat the 0th positional parameter, and the named parameterorElseIterable.forEachat the 0th positional parameterIterable.foldat the 1st positional parameterIterable.lastWhereat the 0th positional parameter, and the named parameterorElseIterable.mapat the 0th positional parameterIterable.reduceat the 0th positional parameterIterable.singleWhereat the 0th positional parameter, and the named parameterorElseIterable.skipWhileat the 0th positional parameterIterable.takeWhileat the 0th positional parameterIterable.whereat the 0th positional parameterList.removeWhereat the 0th positional parameterList.retainWhereat the 0th positional parameterString.replaceAllMappedat the 1st positional parameterString.replaceFirstMappedat the 1st positional parameterString.splitMapJoinat the named parametersonMatchandonNonMatch
BAD:
[1, 3, 5].firstWhere((e) => e.isOdd, orElse: null);
GOOD:
[1, 3, 5].firstWhere((e) => e.isOdd, orElse: () => null);
Enable
#
To enable the null_closures rule, add null_closures under
linter > rules in your analysis_options.yaml
file:
linter:
rules:
- null_closures
If you're instead using the YAML map syntax to configure linter rules,
add null_closures: true under linter > rules:
linter:
rules:
null_closures: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.