Prepare mockito for null safety:
Change [_findAfter] to throw instead of return null, in the case that a match could not be found. The way the code is before, where `firstWhere` can return null via `orElse`, the type of list is inferred to be a list of nullable elements. The migration tool feeds this nullability up very high. Removing the `orElse` and catching the possible StateError is safer.
* Add /*late*/ hints for the migration tool.
PiperOrigin-RevId: 317387499
diff --git a/example/iss/iss.dart b/example/iss/iss.dart
index eef8df4..86e95d5 100644
--- a/example/iss/iss.dart
+++ b/example/iss/iss.dart
@@ -22,7 +22,7 @@
class IssLocator {
final Client client;
- Point<double> _position;
+ /*late*/ Point<double> _position;
Future _ongoingRequest;
IssLocator(this.client);
diff --git a/lib/src/mock.dart b/lib/src/mock.dart
index 9b2e3b1..793f8f6 100644
--- a/lib/src/mock.dart
+++ b/lib/src/mock.dart
@@ -658,9 +658,8 @@
}
RealCall _findAfter(DateTime dt) {
- return matchingInvocations.firstWhere(
- (inv) => !inv.verified && inv.timeStamp.isAfter(dt),
- orElse: () => null);
+ return matchingInvocations
+ .firstWhere((inv) => !inv.verified && inv.timeStamp.isAfter(dt));
}
void _checkWith(bool never) {
@@ -1001,11 +1000,11 @@
_verifyCalls.clear();
var matchedCalls = <RealCall>[];
for (var verifyCall in tmpVerifyCalls) {
- var matched = verifyCall._findAfter(dt);
- if (matched != null) {
+ try {
+ var matched = verifyCall._findAfter(dt);
matchedCalls.add(matched);
dt = matched.timeStamp;
- } else {
+ } on StateError {
var mocks = tmpVerifyCalls.map((vc) => vc.mock).toSet();
var allInvocations =
mocks.expand((m) => m._realCalls).toList(growable: false);
diff --git a/test/invocation_matcher_test.dart b/test/invocation_matcher_test.dart
index dc5bb52..97c8a9e 100644
--- a/test/invocation_matcher_test.dart
+++ b/test/invocation_matcher_test.dart
@@ -153,7 +153,7 @@
///
/// Any call always returns an [Invocation].
class Stub implements Interface {
- static Invocation lastInvocation;
+ static /*late*/ Invocation lastInvocation;
const Stub();