marcojakob / dart-event-bus

An Event Bus using Dart Streams for decoupling applications
MIT License
755 stars 82 forks source link

Provide a way to unsubscribe from events #32

Closed hallidev closed 4 years ago

hallidev commented 4 years ago

I'm using the bloc pattern and register handlers in bloc constructors. When a bloc goes out of scope, I'd like to unregister the handlers in my dispose method. Currently, blocs that fall out of scope keep registering more handlers. This results in "dead" handlers being called.

I've seen this implemented as eventBus.off(handler) in other event bus implementations.

Would it be possible to get this functionality?

marcojakob commented 4 years ago

This is already possible. You need to save the subscription in a variable like this:

StreamSubscription loginSubscription = eventBus.on<UserLoggedInEvent>().listen((event) {
  print(event.user);
});

loginSubscription.cancel();
cnayan commented 11 months ago

@marcojakob It would be great to have this mentioned in the documentation. That way, we don't have to look into issues section on GitHub.

marcojakob commented 11 months ago

@cnayan It's in there already: https://github.com/marcojakob/dart-event-bus#about-dart-streams