ArchitectureHard50 XP4 min read
What is event-driven architecture and how does it apply to Flutter?
TL;DR: Event-driven architecture uses events (not direct calls) to communicate between decoupled components. In Flutter: Streams, StreamControllers, or event_bus package. Blocs communicate via events, not direct method calls.
Full Answer
Direct BLoC-to-BLoC communication creates tight coupling. If CartBloc needs to react to AuthBloc's logout, it shouldn't hold a reference to AuthBloc.
Patterns in Flutter
- ▸StreamController.broadcast(): Publish events, multiple listeners
- ▸event_bus package: Global typed event bus, publish/subscribe pattern
- ▸Riverpod ref.listen(): Listen to other providers and react to changes
- ▸BLoC emit.forEach(): Subscribe to a repository's stream of events
⚠️
Global event buses are powerful but hard to debug — events fire and nobody knows what's listening. Prefer Riverpod's scoped reactivity or emit.forEach for BLoC-to-stream bridges.
Code Examples
dartBLoC reacting to auth events via stream
Output
// CartBloc clears on logout without knowing about AuthBloc // Decoupled: AuthRepository is the only shared dependency
Common Mistakes
- ✗Passing a BLoC reference into another BLoC — creates circular dependencies and hard-to-test code
- ✗Using a global event bus for everything — reserve it for truly cross-cutting events (auth, theme changes)
Interview Tip
💡
Show how emit.forEach() bridges a repository stream into a BLoC. This is the recommended BLoC team pattern for cross-feature communication without direct coupling.
#event-driven#event-bus#stream#decoupling