如何在Flutter中重置Firebase侦听器 [英] How to reset Firebase listeners in Flutter
问题描述
我正在创建一个Flutter应用程序,该应用程序允许用户拥有多个帐户。当用户通过应用程序注销时,它将带他们进入登录屏幕,他们可以登录另一个帐户。我看到的问题是Firebase正在提取新的帐户数据并将其与以前的帐户数据合并。我假设听众没有断开连接,这会导致问题。如何重置Firebase监听器,类似于用户打开应用程序?以下是我的听众的一个例子。谢谢您的帮助或建议。
I am creating a Flutter app that allows a user to have multiple accounts. When the user logout via the app it will bring them to a login screen which they can login to another account. The issue i am seeing is the firebase is pulling in the new account data and merging it with the previous account data. I am assuming that the listeners were not disconnected which cause the issue. How do I reset the Firebase listeners similar to the user opening the app? Below is an example of my listener I have. Thanks for any help or suggestions.
FirebaseDatabase.instance
.reference()
.child("accounts")
..onChildAdded
.listen((event) => OnAddedAction(event)))
..onChildChanged
.listen((event) => OnChangedAction(event)))
..onChildRemoved
.listen((event) => OnRemovedAction(event)))));
推荐答案
您需要存储订阅并致电 cancel()
:
You need to store the subscriptions and call cancel()
on them:
var ref = FirebaseDatabase
.instance
.reference()
.child("accounts");
var sub1 = ref.onChildAdded
.listen((event) => OnAddedAction(event));
var sub2 = ref..onChildChanged
.listen((event) => OnChangedAction(event))
var sub3 = ref.onChildRemoved
.listen((event) => OnRemovedAction(event));
...
@override
dispose() {
sub1?.cancel();
sub2?.cancel();
sub3?.cancel();
super.dispose();
}
这篇关于如何在Flutter中重置Firebase侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!