on<T> method

Stream<T> on<T>()

订阅指定类型事件

类型参数 T:要监听的事件类型

返回值: Stream<T> 事件流,可调用 listen 订阅

用法示例

late StreamSubscription sub;
sub = EventBus().on<MyCustomEvent>().listen((event) {
  print('监听到自定义事件: ${event.msg}');
});

需在不再监听时主动调用 cancel() 取消订阅,避免内存泄漏。

Implementation

Stream<T> on<T>() {
  return _getStreamController<T>().stream;
}