1

I have created a BLoC using flutter_bloc, on which I listen to a stream. When the parent widget gets disposed (and therefore the BLoC object), I would like to close my stream.

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..//
}

Does flutter_bloc or 'Any' class have the equivalent of dispose or Swift's deinit?

Thanks!

1 Answer 1

4

You can override the close method:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.