我已经使用flutter_bloc创建了一个BLoC,我在它上面监听一个流。当父小部件被释放(以及BLoC对象)时,我想关闭我的流。
class ChatBloc extends Bloc<ChatEvent, ChatState> {
//..bloc params..//
ChatBloc(this.chatId) {
this.add(MarkAsRead());
subscription = messagesFirestoreRepository.chatMessages(chatId).listen((messages) {
this.add(UpdateMessages(messages));
});
} //I WANT TO CLOSE THIS WHEN THE BLOC GETS DISPOSED OR DEINITED
//..other stuff..//
}flutter_bloc或'Any‘类有没有等同于dispose或Swift的deinit?
谢谢!
发布于 2020-05-17 23:11:09
您可以覆盖close方法:
class ChatBloc extends Bloc<ChatEvent, ChatState> {
@override
Future<void> close() {
// Release resources here
super.close();
}
}https://stackoverflow.com/questions/61853889
复制相似问题