Skip to content

Instantly share code, notes, and snippets.

@zahidshaikh08
Last active October 12, 2021 12:57
Show Gist options
  • Save zahidshaikh08/85f96d68f7fcfeae658fc21c420ec60d to your computer and use it in GitHub Desktop.
Save zahidshaikh08/85f96d68f7fcfeae658fc21c420ec60d to your computer and use it in GitHub Desktop.
StreamSubscription? subscription;
final eventBus = EventBus();
@override
void initState() {
super.initState();
/// initialize subscription
subscription = eventBus.listenOnce((event) {
/// check for your event type
if (event is MyEvent) {
var data = event.data;
}
});
}
/// cancel subscription
@override
void dispose() {
subscription?.cancel();
super.dispose();
}
class MyEvent {
MyData? data;
MyEvent(this.data);
}
extension EventBusExt<T> on EventBus {
/// Safely listen messages for ONE TIME ONLY
StreamSubscription listenOnce(void Function(dynamic event)? onData) {
return eventBus.streamController.stream.listen(onData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment