Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Flutter BloC to BloC Comminication

Flutter BloC to BloC Comminication

Posted on June 22, 2025 by Toma Velev

There are several valid patterns for Flutter BloC to BloC Comminication. Let’s break them down and summarize the pros and cons of each, plus a few extra notes to guide best practice.


✅ 1. Bloc 1 injected as a property inside Bloc 2

How it works:
BlocB receives an instance of BlocA, listens to its stream, and reacts accordingly.

class BlocB extends Bloc<BlocBEvent, BlocBState> {
  final BlocA blocA;
  late final StreamSubscription subscription;

  BlocB(this.blocA) {
    subscription = blocA.stream.listen((state) {
      if (state is BlocASomeState) add(BlocBEvent());
    });
  }

  @override
  Future<void> close() {
    subscription.cancel();
    return super.close();
  }
}

✅ Pros:

  • Direct and straightforward.
  • Easy to maintain with few blocs.

❌ Cons:

  • Tight coupling between blocs.
  • Harder to test and scale in large systems.

✅ 2. Bloc 2 listens via BlocListener in the UI and passes events to Bloc 2

How it works:
UI uses BlocListener<BlocA> to observe BlocA‘s state and then manually dispatches an event to BlocB.

BlocListener<BlocA, BlocAState>(
  listener: (context, state) {
    if (state is BlocASomeState) {
      context.read<BlocB>().add(BlocBEvent());
    }
  },
  child: YourWidget(),
)

✅ Pros:

  • Decouples the blocs.
  • UI mediates the interaction (good for UI-driven logic).

❌ Cons:

  • UI gets bloated with logic.
  • Hard to reuse in non-UI contexts.

✅ 3. Global Event Bus (e.g. StreamController-based)

How it works:
You define a singleton StreamController<Event> that all blocs can publish to and listen from.

final globalEventBus = StreamController<dynamic>.broadcast();

✅ Pros:

  • Complete decoupling between blocs.
  • Can act as a PubSub pattern across features.

❌ Cons:

  • No type safety (unless you wrap events well).
  • Harder to trace/debug.
  • Easy to misuse — often turns into “god object” anti-pattern.

✅ 4. Global Event Bloc (acts as a shared mediator)

How it works:
You create a GlobalEventBloc that holds global events and states. Other blocs can listen to it or dispatch to it.

class GlobalEventBloc extends Bloc<GlobalEvent, GlobalState> {
  // Dispatch global events here
}

✅ Pros:

  • Centralized, testable, and predictable.
  • All communication stays in the BLoC ecosystem.
  • Good for cross-cutting concerns (auth, session, notifications).

❌ Cons:

  • Introduces a global dependency.
  • Risk of bloat if not scoped properly.

🔄 Comparison Summary:

Method Coupling Testability UI Independence Scale Suitability
Bloc Injected Medium High Yes Small/Medium
UI via BlocListener Low MEDIUM No Small/Medium
Global Event Bus Low LOW Yes LARGE (with discipline)
Global Event Bloc Low-Medium High Yes LARGE

🔔 Bonus: Other related patterns

  • Cubit Delegation: One Cubit calls another Cubit’s method.
  • Repository Mediation: Both blocs depend on a shared repo or service which acts as the sync layer.
  • BlocObserver: Good for side effects like analytics/logging, not for logic transfer.
  • Join iOS Beta Testing Explained
  • Firebase App Distribution Setup
  • iOS App Lifetime Unverified
  • Flutter Bottom Border
  • Get Flutter current time zone

Categories

  • Apps (25)
  • ChatGPT (24)
  • Choosing a Framework (38)
  • Flutter (279)
  • Graphical User Interface (14)
  • Marketing (118)
  • Software Development (288)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (4)
  • Uncategorized (14)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (86)
    • Flutter Apps (26)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Join iOS Beta Testing Explained
  • Firebase App Distribution Setup
  • iOS App Lifetime Unverified
  • Flutter Bottom Border
  • Get Flutter current time zone

Post Categories

  • Apps (25)
  • ChatGPT (24)
  • Choosing a Framework (38)
  • Flutter (279)
  • Graphical User Interface (14)
  • Marketing (118)
  • Software Development (288)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (4)
  • Uncategorized (14)
  • Vaadin (15)