gpt4 book ai didi

flutter - Flutter BLoC(flutter_bloc)是否与onDispose等效?

转载 作者:行者123 更新时间:2023-12-03 04:14:05 26 4
gpt4 key购买 nike

我已经使用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等效的功能?

谢谢!

最佳答案

您可以覆盖close方法:

class ChatBloc extends Bloc<ChatEvent, ChatState> {
@override
Future<void> close() {
// Release resources here
super.close();
}
}

关于flutter - Flutter BLoC(flutter_bloc)是否与onDispose等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com