event_bus
2.0.1, released 2 years ago by jakobservices.ch
A lightweight event bus using Dart Streams for publish/subscribe communication
WASM-ready
What it does
It allows any Dart object to be fired as an event and listened to via streams. Listeners can filter by event type or listen to all events. It supports synchronous and asynchronous buses and custom stream controllers.
AI-written summary of version 2.0.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need a simple global event bus for decoupling components
- You want to avoid complex state management libraries for small projects
- You need to broadcast events across different parts of an app without tight coupling
Skip it when
- You require advanced state management with reactive UI updates
- You need dependency injection or service locator patterns
- You are building a large-scale application that prefers a more structured architecture like BLoC or Redux
Strengths
- Zero dependencies, MIT licensed
- Supports both sync and async buses
- Customizable with any StreamController
- Simple API, easy to integrate
Weaknesses
- No built-in lifecycle or cleanup management
- Not a full state management solution
- Can lead to hard-to-track event flows in large apps
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 0
Add dependency to pubspec and import package:event_bus/event_bus.dart
import 'package:event_bus/event_bus.dart';
final eventBus = EventBus();
class UserLoggedInEvent{final String user;UserLoggedInEvent(this.user);}
eventBus.on<UserLoggedInEvent>().listen((e)=>print(e.user));
eventBus.fire(UserLoggedInEvent('Alice'));Alternatives
Packages that do the same job and could replace event_bus.
Works with
Packages commonly used together with event_bus: ones it builds on, ones built on it, and its own family.
- mqtt_clientBuilt on event_busA Dart MQTT client supporting TCP, WebSocket, secure connections and all QoS levels.
- mqtt5_clientBuilt on event_busA server and browser based MQTT Version 5 client for Dart supporting TCP and WebSockets.
- inspireuiBuilt on event_busA collection of e‑commerce widgets and utilities for Flutter apps.
- coapBuilt on event_busA CoAP (Constrained Application Protocol) library providing an IPv4/v6 client in Dart.
- go_router_modularBuilt on event_busModular Routing and Dependency Injection for Flutter with GoRouter. Features event-driven architecture for seamless module communication and micro-frontend support.
- json_rest_serverBuilt on event_busA RESTful server based on JSON With this package you can have a fully functional RESTful server with auth, pagination and all the necessaries services do build an application