Flutter Bloc:
The Bloc stands for Business Logic Component. it is a pattern created by Google and announced at Google I/O ‘. The BLoC pattern used to handle the flow of data within the app. You add streams of event/data input into a Sink and listen to them as streams of data output through a Stream.
Why Bloc?
Bloc used to separate your presentation from business logic, making your code fast, easy to test, and reusable.
While building production quality applications, managing different states become critical.
As a developer we want to:
- To know what state our application is in at any point in time.
- easily test every case to make sure our app is responding appropriately and properly.
- To record every single user interaction in our application so that it helps to make data-driven decisions to grow business.
- It is useful to work as efficiently as possible and reuse components both within our application and across other applications too.
- To have many developers seamlessly working within a single code base following the same patterns and conventions.
- Used to develop fast and reactive apps.
Bloc was designed to meet all of those needs and many more.
There are many state management solutions and deciding which one to use can be a daunting task.
Bloc was designed to help with the three core values in mind for developers:
- Simple to understand
- It is very easy to understand & can be used by developers with different varying skill levels.
- Powerful for Developers
- Helps to make amazing and complex applications by composing them of smaller components.
- Testable
- We can easily test every aspect of an application so that we can iterate with confidence.
Bloc pattern helps to make state changes predictable by regulating the application when a state change occurs and enforcing a single way to change state throughout an entire application.
Flutter Bloc Pattern Architecture:
Using Bloc pattern we can separate our application into three parts as shown below.
- Data
- Data Provider
- Repository
- Business Logic
- Presentation
Data Layer:
the data layer is used to retrieve and manipulates the data from the sources it may be one or more than one. the data layer is divided into two parts as shown above and it interacts with the database and other network sources and asynchronous methods.
Flutter Bloc (Business Logic) Layer:
The bloc layer’s responsibility is to respond to events from the presentation layer with new states. The bloc layer sends a asyn request to the backend and depend on one or more repositories to retrieve data needed to build up the application state. The Bloc layer gets async response from the backend and sends it to the presentation layer in this way Flutter Bloc layer works.
Flutter Bloc Presentation Layer:
The presentation layer’s responsibility is to render the response from the Bloc layer itself based on one or more bloc states. In addition to this, it handles user input and application lifecycle events.
Core Concepts of Flutter Bloc:
To work with Flutter Blocs we need to understand the core concepts of Bloc before implementing it into the application.
- bloc Events:
- Bloc States
- Bloc Transitions
- Bloc Streams
- Bloc Blocs
- Bloc Delegates
Flutter Bloc Events:
Events are the user input to a Bloc. They are used to get a response to user interactions such as button presses or lifecycle events like page loads. While designing the app we need to define how users will interact with the application. When a user taps on one of the buttons, something will happen to notice the “brains” of our app so that it will respond to the user’s input, this is where the events come into play a major role.
Flutter Bloc States:
States are the output of a Bloc and represent the part of your application’s state. UI components can be notified of states and redraw portions of themselves based on the current state.
To understand the state, go through this example. suppose if we are building a counter app. on this counter app, we have to events one is counterEvent.increment and other is counterEvent.decreament. Now here we have to define the state of the application. Now here we are considering the counter app, so our state is very simple, it’s just an integer which represents the counter’s current value.
Flutter Bloc Transitions:
The changes from one state to another state is called a Transition. A Transition consists of the current states, the event, and the next state.
Flutter Bloc Streams:
A stream is an asynchronous data sequence. To understand the stream consider this example. If you are new to Streams
just think of a pipe with water flowing through it. Here the pipe is the Stream
and the water is the asynchronous data that flows through this stream.
Flutter Blocs:
A Bloc stands for Business Logic Component component which converts a Stream
of incoming Events
into a Stream
of outgoing States
. to get the response to the user inputs.
Flutter Bloc Delegate:
The Beauty of Bloc is that we can have access to all Transitions
in one place. If you want to be able to do something in response to all Transitions
at one place you can simply create your own BlocDelegate
.
Flutter Bloc Widgets:
The BlocBuilder is a Flutter widget needs a Bloc
and a builder
function. BlocBuilder
used to handle building the widget in response to new states. BlocBuilder
is very similar to StreamBuilder
but, it has a simple API to reduce the amount of boilerplate code needed. The builder
function will be called many times and it will be a pure function that returns a widget in response to the state.
See BlocListener
if you want to “do” anything in response to state changes such as navigation, showing a dialog, etc. use the below