搜索插件、代码仓与 APP 分类

插件详情v2.0.1

event_bus

一个使用 Dart Streams 的简单事件总线,用于解耦应用程序

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
35.6万
喜欢数
749
Pub 分数
150

安装插件

flutter pub add event_bus

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

事件总线是一个轻量级的 Dart 包,使用 Streams 实现应用程序中组件之间的事件驱动通信。它通过允许对象发布事件并订阅事件来促进松耦合,而无需直接依赖。非常适合协调应用不同部分的操作,例如 UI 更新、状态变化或跨模块交互。该包易于集成,并可在 Flutter 支持的所有主要平台上无缝运行。

适合场景

  • 解耦UI组件
  • 处理全局状态更改
  • 广播应用范围的通知
  • 简化模块间通信
  • 管理异步事件
  • 减少状态管理中的样板代码

主要特点

  • 基于流的事件传递
  • 简单的发布/订阅模式
  • 跨平台兼容性
  • 最小 API 表面
  • 支持类型化事件
  • 无外部依赖

适合对象

  • 需要松耦合的应用程序
  • 开发人员避免使用复杂的状态管理器
  • 频繁进行组件间通信的项目
  • 快速事件流原型设计
  • 倾向于简单性而非配置的团队

使用建议

  • 不适合大规模状态管理
  • 有限的内置错误处理
  • 需要仔细命名事件以避免冲突
  • 不会在应用重启后保留事件
  • 最适合与其他状态管理解决方案配合使用,以应对复杂应用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Event Bus

A simple Event Bus using Dart Streams for decoupling applications.

Star this Repo Pub Package

GitHub | Pub | Demos and Examples

Event Bus Pattern

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.

Event Bus and MVC

The Event Bus pattern is especially helpful for decoupling MVC (or MVP) applications.

One group of MVC is not a problem.

Model-View-Controller

But as soon as there are multiple groups of MVCs, those groups will have to talk to each other. This creates a tight coupling between the controllers.

Multi Model-View-Controllers

By communication through an Event Bus, the coupling is reduced.

Event Bus

Usage

1. Create an Event Bus

Create an instance of EventBus and make it available to other classes.

Usually there is just one Event Bus per application, but more than one may be set up to group a specific set of events.

import 'package:event_bus/event_bus.dart';

EventBus eventBus = EventBus();

Note: The default constructor will create an asynchronous event bus. To create a synchronous bus you must provide the optional sync: true attribute.

2. Define Events

Any Dart class can be used as an event.

class UserLoggedInEvent {
  User user;

  UserLoggedInEvent(this.user);
}

class NewOrderEvent {
  Order order;

  NewOrderEvent(this.order);
}
3. Register Listeners

Register listeners for specific events:

eventBus.on<UserLoggedInEvent>().listen((event) {
  // All events are of type UserLoggedInEvent (or subtypes of it).
  print(event.user);
});

Register listeners for all events:

eventBus.on().listen((event) {
  // Print the runtime type. Such a set up could be used for logging.
  print(event.runtimeType);
});
About Dart Streams

EventBus uses Dart Streams as its underlying mechanism to keep track of listeners. You may use all functionality available by the Stream API. One example is the use of StreamSubscriptions to later unsubscribe from the events.

StreamSubscription loginSubscription = eventBus.on<UserLoggedInEvent>().listen((event) {
  print(event.user);
});

loginSubscription.cancel();
4. Fire Events

Finally, we need to fire an event.

User myUser = User('Mickey');
eventBus.fire(UserLoggedInEvent(myUser));

Using Custom Stream Controllers

Instead of using the default StreamController you can use the following constructor to provide your own.

An example would be to use an RxDart Subject as the controller.

import 'package:rxdart/rxdart.dart';

EventBus behaviorBus = EventBus.customController(BehaviorSubject());

Running / Building / Testing

  • Run from the terminal: webdev serve
  • Build from the terminal: webdev build
  • Testing: pub run build_runner test -- -p chrome

License

The MIT License (MIT)

所属分类

源码仓库

使用此插件的项目