The flutter bloc pattern has become one of the most popular design patterns in the flutter community. At almost 4,000 stars on GitHub (at the time this article was written), bloc package provides a powerful tool that helps you build build reactive and maintainable mobile applications.

So what exactly is block and why do people use it?

Bloc stands for business logic component and it is used for managing state within an application. This is so all the things that change within the screen can be handled from one component, instead of handled throughout the app, in various locations.

Let’s get into the core components of the flutter bloc package.

BLoC

Bloc is the what is handling all the interactions between the UI and any business logic. Bloc takes events as inputs, runs whatever algorithm is necessary for those events and returns a state. It’s as simple as that.

Events

Events are what gets sent to the Bloc to be acted upon. Events can be triggered from some actions happening in the software, whether a button click or data received. Based on the event that gets sent, the bloc should execute some logic.

States

Once the logic is executed the bloc needs to return a state. A state is something that can change within an application. Whether it is on the actual UI or in the background(like authentication being received).

BlocBuilder

Once a state has been received, it needs to be handled, and widgets need to be rebuilt accordingly. This is what the BlocBuilder is for. Whenever a state change happens, it will rebuild all the child widgets with the new state information.

BlocProvider

What if your Bloc is used in multiple places? That’s where the BlocProvider comes in. BlocProvider is like an InheritedWidget for blocs. All the widgets in its subtree will have access to the bloc that has been instantiated in the BlocProvider.

With all these together you have the basics of BLoC Pattern. With these basics you can create software that is clean, with the business logic separated from the the UI completely. You will be able to achieve a clean architecture and have code that is more maintainable and easier to debug.

I would like to thank Felix Angelov, who is the manager of the flutter bloc package. I will keep learning more about BLoC and the more advanced aspects like handling multiple blocs. So follow me on medium or subscribe to my YouTube channel for more!

Link to flutter_bloc: https://pub.dev/packages/flutter_bloc

Thanks for Reading!

Follow me on Twitter: @tadaspetra