简体   繁体   中英

Flutter StreamSubscription cancel() not canceling in Stateless widget

I have this Streamsubscription in a Stateless widget

StreamSubscription < LocationResult > subscription = 
Geolocation.locationUpdates(
accuracy: LocationAccuracy.best,
displacementFilter: 30.0, // in meters
inBackground: true,)
.listen((result) {
if (result.isSuccessful) {         
saveResult(result);
} else {
}
});

I call this in a timer

startTimeout(mins) async {
await subscription.resume();
print("susbscription started");
return new Timer(Duration(minutes: mins), handleTimeout);
}
void handleTimeout() async{  // callback function
await subscription.cancel();
print("susbscription canceled");
}

The timer is called on button click:

startTimeout(1);

After a minute susbscription canceled is printed but the Geolocator keeps getting called.

Future cancel ()

Cancels this subscription.

After this call, the subscription no longer receives events.

The stream may need to shut down the source of events and clean up after the subscription is canceled.

Returns a future that is completed once the stream has finished its cleanup.

For historical reasons, may also return null if no cleanup was necessary. Returning null is deprecated and should be avoided.

Typically, futures are returned when the stream needs to release resources. For example, a stream might need to close an open file (as an asynchronous operation). If the listener wants to delete the file after having canceled the subscription, it must wait for the cleanup future to complete.

A returned future completes with a null value. If the cleanup throws, which it really shouldn't, the returned future completes with that error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM