gpt4 book ai didi

firebase - Flutter StreamSubscription 没有停止或暂停

转载 作者:行者123 更新时间:2023-12-03 02:49:52 30 4
gpt4 key购买 nike

在我的 Flutter 应用中,StreamSubscription 没有暂停或取消。当我调用 cancel() 如果它之前启动时,它将停止。如果我在启动后调用 cancel(),它不会停止。我正在使用 Firestore 快照监听器。下面是我的代码。我尝试了不同的方法,但仍然无法正常工作。问题是 Firestore listener 在加载数据后没有停止。

    StreamSubscription<QuerySnapshot> streamSubscription;

@override
void initState() {
super.initState();

print("Creating a streamSubscription...");
streamSubscription =Firestore.collection("name").document("d1").collection("d1")
.snapshots().listen((data){
//It will display items
}, onDone: () { // Not excecuting
print("Task Done");
}, onError: (error) {
print("Some Error");
});
streamSubscription.cancel(); //It will work but cancel stream before loading
}

@override
void dispose() {
streamSubscription.cancel(); //Not working
super.dispose();
}

最佳答案

当你推送一个新页面时,前一个页面仍然被渲染,因此 dispose() 不会被调用。

此外,有时可能会发生小部件不再呈现但 dispose 尚未调用的情况,这可能会导致奇怪的错误消息。因此,如果您使用 dispose,添加这样的检查可能也是一个好主意。

改变

//It will display items

if(myIsCurrentRoute && mounted) {
//It will display items
}

关于firebase - Flutter StreamSubscription 没有停止或暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54899927/

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