gpt4 book ai didi

dart - 从回调内部取消 StreamSubscription

转载 作者:行者123 更新时间:2023-12-03 03:17:53 31 4
gpt4 key购买 nike

这是一个 (简化)场景 情况的:

stream.listen((bool result) {
if (result) {
// should cancel the subscription
}
});

我想停止听 Stream基于它的内容,但我不能完全理解它以得出结论。
StreamSubscription streamSubscription = stream.listen((_) {});

streamSubscription.cancel(); // cancels the subscription

cancel()我通常可以取消订阅,但无法访问 streamSubscriptionlisten打回来。

最佳答案

您需要拆分变量声明和初始化:

StreamSubscription streamSubscription;
streamSubscription = stream.listen((bool result) {
if (result) {
streamSubscription.cancel();
}
});

关于dart - 从回调内部取消 StreamSubscription,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233201/

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