@@ -86,7 +86,8 @@ void main() {
8686 await GoogleMapsNavigator .stopGuidance ();
8787 }
8888
89- GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
89+ final StreamSubscription <OnArrivalEvent > onArrivalSubscription =
90+ GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
9091
9192 /// Simulate location and test it.
9293 await setSimulatedUserLocationWithCheck (
@@ -132,9 +133,11 @@ void main() {
132133 expectSync (msg.location.longitude, lessThanOrEqualTo (endLng + tolerance));
133134 }
134135
135- await GoogleMapsNavigator .setRoadSnappedLocationUpdatedListener (
136- onLocationEvent,
137- );
136+ final StreamSubscription <RoadSnappedLocationUpdatedEvent >
137+ roadSnappedSubscription =
138+ await GoogleMapsNavigator .setRoadSnappedLocationUpdatedListener (
139+ onLocationEvent,
140+ );
138141
139142 /// Start simulation.
140143 await GoogleMapsNavigator .simulator.simulateLocationsAlongExistingRoute ();
@@ -143,6 +146,9 @@ void main() {
143146 await hasArrived.future;
144147 expect (await GoogleMapsNavigator .isGuidanceRunning (), false );
145148
149+ // Cancel subscriptions before cleanup
150+ await onArrivalSubscription.cancel ();
151+ await roadSnappedSubscription.cancel ();
146152 await GoogleMapsNavigator .cleanup ();
147153 });
148154
@@ -228,7 +234,8 @@ void main() {
228234 }
229235 }
230236
231- GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
237+ final StreamSubscription <OnArrivalEvent > onArrivalSubscription =
238+ GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
232239
233240 /// Simulate location and test it.
234241 await setSimulatedUserLocationWithCheck (
@@ -291,9 +298,11 @@ void main() {
291298 }
292299 }
293300
294- await GoogleMapsNavigator .setRoadSnappedLocationUpdatedListener (
295- onLocationEvent,
296- );
301+ final StreamSubscription <RoadSnappedLocationUpdatedEvent >
302+ roadSnappedSubscription =
303+ await GoogleMapsNavigator .setRoadSnappedLocationUpdatedListener (
304+ onLocationEvent,
305+ );
297306
298307 /// Start simulation.
299308 $.log ('Starting simulation with speedMultiplier: 5' );
@@ -311,6 +320,9 @@ void main() {
311320 await navigationFinished.future;
312321 expect (await GoogleMapsNavigator .isGuidanceRunning (), false );
313322
323+ // Cancel subscriptions before cleanup
324+ await onArrivalSubscription.cancel ();
325+ await roadSnappedSubscription.cancel ();
314326 await GoogleMapsNavigator .cleanup ();
315327 },
316328 variant: multipleDestinationsVariants,
@@ -471,8 +483,8 @@ void main() {
471483 await finishTest.future;
472484 $.log ('Loop with simulator$loopIteration finished.' );
473485
474- await GoogleMapsNavigator .cleanup ();
475486 await subscription.cancel ();
487+ await GoogleMapsNavigator .cleanup ();
476488 await $.pumpAndSettle ();
477489 }
478490 });
@@ -630,7 +642,13 @@ void main() {
630642 expect (GoogleMapsNavigator .isGuidanceRunning (), false );
631643 }
632644
633- GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
645+ final StreamSubscription <OnArrivalEvent > onArrivalSubscription =
646+ GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
647+
648+ // The subscription will be automatically cleaned up when the test ends.
649+ addTearDown (() async {
650+ await onArrivalSubscription.cancel ();
651+ });
634652 });
635653
636654 patrol ('Test error during navigation if no network connection' , (
@@ -753,7 +771,8 @@ void main() {
753771 hasArrived.complete ();
754772 }
755773
756- GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
774+ final StreamSubscription <OnArrivalEvent > onArrivalSubscription =
775+ GoogleMapsNavigator .setOnArrivalListener (onArrivalEvent);
757776
758777 /// Simulate location (1298 California St)
759778 const double tolerance = 0.001 ;
@@ -850,6 +869,9 @@ void main() {
850869
851870 /// Check that the last segment is near target destination.
852871 expect (endSegment! .destinationLatLng.longitude, closeTo (- 122.412 , 0.002 ));
872+
873+ // Cancel subscription before test ends
874+ await onArrivalSubscription.cancel ();
853875 });
854876
855877 patrol ('Test that the navigation session is attached to existing map' , (
0 commit comments