EventBus class
EventBus
说明
全局自定义事件总线,实现类似 iOS NotificationCenter 的事件分发机制, 用于在应用内跨 Widget/模块之间发送和监听事件通知。
适用于解耦各个模块间的事件通信需求。
单例用法
通过 EventBus() 获取单例对象。
快速示例
// 发送事件
EventBus().send(ThemeChangedEvent('follow system : darkTheme'));
// 订阅事件(一般在 initState)
late StreamSubscription sub;
sub = EventBus().on<ThemeChangedEvent>().listen((event) {
print('收到主题变更事件: ${event.theme}');
});
// 取消订阅(一般在 dispose)
sub.cancel();
Constructors
- EventBus()
-
工厂构造函数,返回同一个实例。
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
dispose(
) → void - 释放所有事件流(StreamController)
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on<
T> () → Stream< T> - 订阅指定类型事件
-
send<
T> (T event) → void - 发送事件通知
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited