on<T> static method

StreamSubscription<T> on<T>(
  1. void handler(
    1. T event
    ), {
  2. bool handleError = true,
  3. ErrorCallback? onError,
})

订阅事件,返回可取消的订阅对象

Implementation

static StreamSubscription<T> on<T>(
  void Function(T event) handler, {
  bool handleError = true,
  ErrorCallback? onError,
}) {
  final subscription = instance.on<T>().listen(
    (event) {
      if (kDebugMode) {
        print('[EventBus] Received event: ${event.runtimeType}');
      }
      _safeRun(() => handler(event), onError: onError);
    },
    onError: handleError
        ? (error, stack) {
            _safeRun(() => onError?.call(error, stack));
          }
        : null,
  );

  return subscription;
}