Easy EventBus 是一個輕量級、直覺的事件總線庫,專為 Flutter 應用程式設計,透過事件實現元件間的簡單高效通訊。它提供優雅的 API 用於發佈和訂閱事件,避免緊密耦合,非常適合狀態管理與元件間訊息傳遞。該庫支援所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,確保跨平台的一致性。透過最少的設定與乾淨的語法,提升複雜應用中的程式碼可維護性與可擴展性。
適用情境
即時通知
跨組件通訊
UI 與業務邏輯解耦
事件驅動架構
跨螢幕的狀態同步
外掛程式或模組整合
主要特色
簡單的發佈/訂閱模式
跨平台支援
最小化樣板程式碼
類型安全的事件處理
輕量級依賴
乾淨且易讀的 API
適合對象
尋求輕量級狀態管理的開發者
頻繁組件間通訊的應用
需要解耦架構的專案
重視程式碼簡潔性的團隊
Flutter 初學者探索事件驅動設計
使用建議
不適合大規模狀態管理
有限的內建錯誤處理
無內建持久性
需要仔細命名事件以避免衝突
最佳搭配其他狀態管理解決方案使用
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
EasyEventBus
It's an easy-to-use and elegant library based on event bus. It was greatly inspired by EventBus and made much simpler.
About Event Bus
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.
The Event Bus pattern is especially helpful for decoupling MVC
(or MVP) applications.
One group of MVC is not a problem.
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.
By communication through an Event Bus, the coupling is reduced.
Usage
Step 1: Subscribe to an Event
First, set up a subscription for the desired event. This allows you to listen for and react to the event whenever it is triggered.
EasyEventBus.on('sayHello', (event) {
print(event); // write your code here.
});
Step 2: Trigger the Event
Next, trigger the event at the appropriate time in your application. This will notify all subscribers of the event.
EasyEventBus.fire('sayHello', 'hello world'); // Triggers the event with a any data.
Note: The event data can be of any type, giving you flexibility in what information you pass to the subscribers.
That's All!
Advance
1. Cancel Subscription
To cancel a subscription, you call the cancel method. This example shows how to cancel all subscriptions for the 'sayHello' event, both anonymous and with identifiers.
// Cancel all subscriptions to the 'sayHello' event.EasyEventBus.cancel('sayHello');
2. Cancel Specific Subscription
Before canceling a specific subscription, you first need to create a subscription with an identifier (id). Here is how to subscribe to an event with an id and then cancel that specific subscription by calling the cancel method with the same id.
First, subscribe to an event with an id:
// Subscribe to the 'sayHello' event with an identifier.EasyEventBus.on('sayHello', (event) {
print('Received: $event');
}, id: 'uniqueId123');
Then, cancel the subscription using the same id:
// Cancel the subscription to the 'sayHello' event using the identifier 'uniqueId123'.EasyEventBus.cancel('sayHello', id: 'uniqueId123');
3. Cancel All Subscriptions
You can cancel all subscriptions by calling the cancelAll method. This method cancels all subscriptions for all events and closes all event controllers.
// Cancel all subscriptions and close all event controllers.EasyEventBus.cancelAll();