Member-only story

Cubit to Cubit Communication

Syed Abdul Basit
4 min readOct 14, 2024

--

In this article, we will explore the communication between Cubits. We’ll delve into the approach using StreamSubscription, providing clear examples to illustrate this method. Let’s dive in!

Understanding Cubits

Before we dive into the communication methods, let’s quickly recap what Cubits are. Cubits are a part of the Bloc library, which helps manage state in Flutter applications. They are lightweight and provide a way to emit new states to the UI based on function.

Communication Using StreamSubscription

Using StreamSubscription to listen for state changes in one Cubit and react accordingly in another. This approach allows for responsive updates based on changes in the state of another Cubit.

+-------------------+
| Start |
+-------------------+
|
v
+-------------------+
| TodoListCubit |
| emits new state |
+-------------------+
|
v
+-------------------+
| ActiveTodoCountCubit |
| subscribes to TodoListCubit |
+-------------------+
|
v
+-------------------+
| Count active todos |
| Emit active count |
+-------------------+
|
v
+-------------------+
| End |
+-------------------+

Example: StreamSubscription

--

--

Responses (1)