Last active
October 12, 2021 12:57
-
-
Save zahidshaikh08/85f96d68f7fcfeae658fc21c420ec60d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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