Flutter BLoC(flutter_bloc)是否等效于onDispose?

问题描述 投票:0回答:1

我已经使用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?

谢谢!

flutter dart stream flutter-bloc
1个回答
0
投票

您可以覆盖close方法:

class ChatBloc extends Bloc<ChatEvent, ChatState> {
  @override
  Future<void> close() {
    // Release resources here
    super.close();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.