gpt4 book ai didi

dart - Flutter StreamSubscription cancel()在无状态小组件中未取消

转载 作者:行者123 更新时间:2023-12-03 03:05:32 27 4
gpt4 key购买 nike

我在无状态小部件中有此Streamsubscription

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

我称它为计时器
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");
}

在单击按钮时调用计时器:
startTimeout(1);

一分钟后取消取消打印,但Geolocator继续被 call 。

最佳答案

Future cancel ()

取消此订阅。

在此 call 之后,订阅不再接收事件。

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.

关于dart - Flutter StreamSubscription cancel()在无状态小组件中未取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54577026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com