rx_event_bus 2.0.1 copy "rx_event_bus: ^2.0.1" to clipboard
rx_event_bus: ^2.0.1 copied to clipboard

A EventBus using RxDart

rx_event_bus #

Pub Awesome Flutter Awesome Flutter License

基于 RxDart 的事件总线。

安装 #

pubspec.yaml 添加依赖

dependencies:
  rx_event_bus: <last_version>

如何使用 #

因为 Dart 支持顶级函数, 所以可以直接定义

EventBus eventBus = EventBus();

然后定义通讯数据

class OnAppState {
  int state;
  const OnAppState(this.state); 
}

🛠 Base Broadcast #

1、注册接收器

eventBus.on<OnAppState>().listen((event) {
  print('event: ${event.state}');
});

2、发送事件通知

eventBus.fire(OnAppState('paused'));

🧲 Sticky Broadcast #

当👆上面的基础用法的发送事件通知在注册接收器前出发, 则消息会丢失, 如果需要得到消息, 则可使用粘性广播.

1、注册接收器

eventBus.onSticky<OnAppState>().listen((event) {
  print('event: ${event.state}');
});

2、发送事件通知

设置 stickytrue 则可发送粘性广播.

eventBus.fire(OnAppState('paused'), sticky: true);

注意: 粘性广播默认只会保留上一次的数据, 如需要自定义条数, 可使用如下方式:

EventBus eventBus = EventBus(maxSize: 10);
  • maxSize: 粘性广播最大的留存条数