Flutter Event bus utils

An Event Bus follows the publish/subscribe pattern. It allows listeners to subscribe for events and publishers to fire events. This enables objects to interact without requiring to explicitly define listeners and keeping track of them.

import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/widgets.dart';

/*
HosEventBusUtils.fire('refresh');
HosEventBusUtils.off();

HosEventBusUtils.on((event) {
      if (event == 'refresh') { //SPECIFIC EVENT GET
       
      }
    });
*/

class HosEventBusUtils {
  static EventBus _eventBus;
 
  static EventBus shared() {
    if (_eventBus == null) {
      _eventBus = EventBus();
    }
    return _eventBus;
  }

  static Map<Type, List<StreamSubscription>> subscriptions = {};

  static StreamSubscription on<T extends Object>(void onData(T event),
      {Function onError,
      void onDone(),
      bool cancelOnError,
      bool autoManaged = true}) {
    StreamSubscription subscription = shared()?.on<T>()?.listen(onData,
        onError: onError, onDone: onDone, cancelOnError: cancelOnError);
    if (autoManaged == true) {
      if (subscriptions == null) subscriptions = {};
      List<StreamSubscription> subs = subscriptions[T.runtimeType] ?? [];
      subs.add(subscription);
      subscriptions[T.runtimeType] = subs;
    }
    return subscription;
  }

  static void off<T extends Object>({StreamSubscription subscription}) {
    if (subscriptions == null) subscriptions = {};
    if (subscription != null) {
      List<StreamSubscription> subs = subscriptions[T.runtimeType] ?? [];
      subs.remove(subscription);
      subscriptions[T.runtimeType] = subs;
    } else {
      subscriptions[T.runtimeType] = null;
    }
  }

  static void fire(event) {
    shared()?.fire(event);
  }
}

mixin HosEventBusMixin<T extends StatefulWidget> on State<T> {
  EventBus mEventBus = HosEventBusUtils.shared();
  List<StreamSubscription> mEventBusSubscriptions = [];

  @protected
  void mAddEventBusListeners();

  void mAddEventBusListener<T>(void onData(T event),
      {Function onError, void onDone(), bool cancelOnError}) {
    mEventBusSubscriptions?.add(mEventBus?.on<T>()?.listen(onData,
        onError: onError, onDone: onDone, cancelOnError: cancelOnError));
  }

  void mEventBusFire(event) {
    mEventBus?.fire(event);
  }
 
  @override
  @mustCallSuper
  void dispose() {
    super.dispose();
    if (mEventBusSubscriptions != null)
      for (StreamSubscription subscription in mEventBusSubscriptions) {
        subscription.cancel();
      }
  }
 
  @override
  @mustCallSuper
  void initState() {
    super.initState();
    mAddEventBusListeners();
  }
}