我已经使用flutter_bloc创建了一个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?
谢谢!
您可以覆盖close
方法:
class ChatBloc extends Bloc<ChatEvent, ChatState> {
@override
Future<void> close() {
// Release resources here
super.close();
}
}