[英]Flutter Firebase: How to remove firebase listener when using stream builder?
Every example about StreamBuilder
starts with a StatelessWidget
even in flutter
example but how do you cancel the subscription in a StatelessWidget
widget? 大约每例如
StreamBuilder
有开始StatelessWidget
即使在flutter
的例子,但你怎么取消在订阅StatelessWidget
小部件? For example, I was going through firestore
example . 例如,我正在研究
firestore
示例 。
class MessageList extends StatelessWidget {
MessageList({this.firestore});
final Firestore firestore;
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: firestore.collection('messages').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int messageCount = snapshot.data.documents.length;
return ListView.builder(
itemCount: messageCount,
itemBuilder: (_, int index) {
final DocumentSnapshot document = snapshot.data.documents[index];
return ListTile(
title: Text(document['message'] ?? '<No message retrieved>'),
subtitle: Text('Message ${index + 1} of $messageCount'),
);
},
);
},
);
}
}
Now how do I cancel listening to firestore.collection('messages').snapshots()
stream? 现在如何取消收听
firestore.collection('messages').snapshots()
流?
I use realtime database in my app and this is how I do it 我在应用程序中使用实时数据库,这就是我的方法
class MessgaeView extends StatefulWidget {
final String _chatId;
MessgaeView(this._chatId);
@override
_MessgaeViewState createState() => _MessgaeViewState();
}
class _MessgaeViewState extends State<MessgaeView> {
Stream<Event> _messageStream;
@override
void initState() {
_messageStream = _database
.reference()
.child("message/${widget._chatId}")
.limitToLast(1)
.onChildAdded;
super.initState();
}
@override
void dispose() {
_messageStream.drain();
super.dispose();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _messageStream,
builder: (context, snapshot) {
if (!snapshot.hasData) return CupertinoActivityIndicator();
final message =
(snapshot.data.snapshot as DataSnapshot).value['message'];
return Text(message);
});
}
}
Simply replace the previous stream instance with null
. 只需将前一个流实例替换为
null
。
This will require a code similar to the following somewhere: 这将需要类似于以下内容的代码:
setState(() {
_messageStream = null;
});
Doing so will stop listening to the stream. 这样做将停止收听流。 But
StreamBuilder
will still hold the previous value. 但是
StreamBuilder
仍将保留以前的值。
I hava same problem and solved by StreamSubscription 我遇到了同样的问题,并通过StreamSubscription解决了
For example define StreamSubscription as global 例如,将StreamSubscription定义为全局
StreamSubscription<Event> _counterSubscription;
then in your place you want to listen to data register your Subscription like this 然后在您要收听的地方,像这样注册您的订阅
_counterSubscription = _counterRef.onValue.listen((Event event) {
setState(() {
_counter = event.snapshot.value ?? 0;
});
});
and when you want to remove listener just make this code 而当您想删除监听器时,只需编写以下代码
if(_counterSubscription !=null)
_counterSubscription .cancel();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.