Blocs and streams (emit.forEach)
Occasionally, we utilize blocs that observe a continuous flow of data. Typically, this flow originates from a repository within our domain layer, where it can be observed by several blocs. These blocs then leverage the data stream to inform their operational decisions based on business logic.
When dealing with streams in Bloc versions 7.2.0 and above, we can leverage the functionalities offered by the emit object to streamline our interaction with streams. The
Here's an example illustrating how much
#info #tip
Occasionally, we utilize blocs that observe a continuous flow of data. Typically, this flow originates from a repository within our domain layer, where it can be observed by several blocs. These blocs then leverage the data stream to inform their operational decisions based on business logic.
When dealing with streams in Bloc versions 7.2.0 and above, we can leverage the functionalities offered by the emit object to streamline our interaction with streams. The
emit.forEach
method enables us to receive a callback whenever there is data in the stream. Moreover, when our event handler is terminated or completes its task, it automatically handles the cleanup process by ending the stream subscription. Here's an example illustrating how much
emit.forEach
method saves us from writing repetitive boilerplate code when utilizing stream in bloc.#info #tip
❤4👍3