英文:
Override dispose method in a ChangeNotifier
问题
我有以下的模型
class SelectorModel with ChangeNotifier {
.... // 与模型相关的内容
}
在我的模型内部,我使用以下方式获取Firestore文档的流:
_subscription = Firestore.instance // _subscription在上面被定义为一个iVar
.collection('myCollection')
.snapshots()
.listen((querySnapshot) {
_jobs = querySnapshot.documents;
callingMethod(''); // 被调用的方法在我的模型内部
});
我需要一种方法来释放 _subscription
,以便当我的Firestore数据库发生更改时,不会尝试在我的模型已经被释放时调用 callingMethod()
。
我正在寻找的方法几乎类似于以下的ChangeNotifier方法:
@override
void dispose() {
super.dispose();
_subscription.cancel();
}
我查看了Provider的文档,但没有找到相关内容。
感谢任何帮助!
英文:
I have the following model
class SelectorModel with ChangeNotifier {
.... // stuff relating to the model
}
and inside my model I'm getting a stream of Firestore documents using the following:
_subscription = Firestore.instance // _subscription is defined as an iVar above
.collection('myCollection')
.snapshots()
.listen((querySnapshot) {
_jobs = querySnapshot.documents;
callingMethod(''); // the method being called is inside of my model
});
I need a way to dispose of the _subsciption
so that when a change occurs in my Firestore database, it will not attempt to call callingMethod()
when my model has already been disposed.
What I'm looking for is a method almost like the following for a ChangeNotifier:
@override
void dispose() {
super.dispose();
_subscription.cancel();
}
I looked through the provider docs but could not find anything.
Thanks for any help!
答案1
得分: 1
你可以这样做,如果我理解你的意思
_subscription = Firestore.instance
.collection('myCollection')
.snapshots()
.listen((querySnapshot) {
_jobs = querySnapshot.documents;
callingMethod(''); // 被调用的方法在我的模型内部
_subscription.cancel();
});
英文:
You can do it like that if i understand you
_subscription = Firestore.instance // _subscription is defined as an iVar above
.collection('myCollection')
.snapshots()
.listen((querySnapshot) {
_jobs = querySnapshot.documents;
callingMethod(''); // the method being called is inside of my model
_subscription.cancel();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论