Pattern: Use of null
as argument where closure is expected
Issue: -
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:
- From
dart:async
Future
at the 0th positional parameterFuture.microtask
at the 0th positional parameterFuture.sync
at the 0th positional parameterTimer
at the 0th positional parameterTimer.periodic
at the 1st positional parameter- From
dart:core
List.generate
at the 1st positional parameter
- From
dart:async
sheduleMicrotask
at the 0th positional parameterFuture.doWhile
at the 0th positional parameterFuture.forEach
at the 0th positional parameterFuture.wait
at the named parametercleanup
Timer.run
at the 0th positional parameter
- From
dart:async
Future.then
at the 0th positional parameterFuture.complete
at the 0th positional parameter- From
dart:collection
Queue.removeWhere
at the 0th positional parameter- `Queue.retain
Iterable.firstWhere
at the 0th positional parameter, and the named parameterorElse
Iterable.forEach
at the 0th positional parameterIterable.fold
at the 1st positional parameterIterable.lastWhere
at the 0th positional parameter, and the named parameterorElse
Iterable.map
at the 0th positional parameterIterable.reduce
at the 0th positional parameterIterable.singleWhere
at the 0th positional parameterIterable.skipWhile
at the 0th positional parameterIterable.takeWhile
at the 0th positional parameterIterable.where
at the 0th positional parameterList.removeWhere
at the 0th positional parameterList.retainWhere
at the 0th positional parameterString.replaceAllMapped
at the 1st positional parameterString.replaceFirstMapped
at the 1st positional parameterString.splitMapJoin
at the named parametersonMatch
andonNonMatch
Example of incorrect code:
[1, 3, 5].firstWhere((e) => e.isOdd, orElse: null);
Example of correct code:
[1, 3, 5].firstWhere((e) => e.isOdd, orElse: () => null);