null passed where the API requires a callback
ID |
dart.correctness.null_closures |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Contract |
Language |
Dart |
Description
Reports a literal null passed in an argument position that a core-library method requires to be a function, such as the test of firstWhere or where, the transform of map or expand, the combiner of reduce and fold, the factory of Map.putIfAbsent or List.generate.
Rationale
These parameters are not optional and have no default: the method invokes the argument unconditionally. Passing null therefore does not disable the callback, it produces a NoSuchMethodError the first time the element is reached. The failure surfaces deep inside the collection call rather than at the call site, and because it needs a non-empty collection to trigger, it can pass every test that happens to exercise the empty case.
The following code illustrates the pattern detected by this rule:
class Catalogue {
List<int> prices = [3, 7, 11];
Map<String, int> stock = {};
int cheapest() {
// FLAGGED: null passed where the API requires a callback
return prices.firstWhere(null);
}
}