Flutter BLoC Tutorial For Absolute Beginners
Tags: flutter, tutorial
Published: 2022-11-24 11:00:00

##What Is Flutter Bloc?

In Flutter applications, the Flutter BLoC is used to manage the state. The [flutter state management](https://www.solutelabs.com/blog/flutter-state-management-everything-you-need-to-know) feature allows handling all possible states of the application easily. It is easy to grasp the concept of the Flutter bloc. This library has excellent [documentation](https://bloclibrary.dev/#/) with a lot of examples. It's also one of the most commonly used libraries in the Flutter community.

It is powerful since it allows the creation of all kinds of applications. For instance, the Flutter bloc is appropriate in both production and learning environments.

##Flutter Bloc: An Overview

There are several pub packages included in the Bloc:

![Flutter Bloc An Overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yseih2x1q3q1ksz97w5f.png)

It is necessary to install the [Dart SDK](https://dart.dev/get-dart) on the computer before one can use bloc.

##Installation

First, our pubspec.yaml needs to include the bloc package as a dependency for a [Dart](https://dart.dev/) application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/el8xkhu46s6s3bm2m6d0.png)

As a dependency, the flutter_bloc package needs to be added to pubspec.yaml for a Flutter application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecqc69ryrj2jkn5r26ih.png)

Similarly, as a dependency, the angular_bloc package needs to be added to pubspec.yaml for an AngularDart application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3xrhdw2tla3b13iywhi.png)

The next step is to [install](https://bloclibrary.dev/#/gettingstarted?id=installation) Bloc.

If it does not have a pubspec.yaml file, make sure to run this command from the same directory as pubspec.yaml.

- If using Dart or AngularDart,**run** pub get
- If using Flutter, then run **flutter packages get**

##Import

We can now import bloc into main.dart after successfully installing bloc.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck8o9jszh8oq7je86bjh.png)

##Bloc

A [Bloc](https://bloclibrary.dev/#/coreconcepts?id=bloc) is a more advanced class that incorporates events rather than functions to trigger state changes. Unlike Cubit, Bloc also extends BlocBase, which means its API is similar. A Bloc receives an event, which then changes the incoming event into an outgoing state rather than directly calling a function on the Bloc and emitting a new state.

###Creating A Bloc

A Bloc can be created to specify what events it will process. Blocs receive events as input. They are often added when users interact with a page, such as by pressing buttons. As with Blocs, Cubits also define the state they will manage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hlblly4sne8hj7ottys.png)

The superclass must also be passed an initial state, just as when creating the CounterCubit.

###State Changes

In Bloc, event handlers are registered using the on<Event> API. Any incoming event is converted into one or more outgoing states by an event handler.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g06zic7wgmw0aim5dw2x.png)

The EventHandler can then be updated to handle CounterIncrementPressed

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22ff99sus702vri0pfc1.png)

In the above snippet, we are registering an EventHandler to handle CounterIncrementPressed events. A state getter and emit(state + 1) are available to access the current bloc state for an incoming CounterIncrementPressed event.

###Using a Bloc

Let's create a CounterBloc instance and use it!

####Basic Usage

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g50f5xxahzn3ddyzcm1d.png)

As shown in the above code snippet, we create a CounterBloc instance. This is the initial state of the Bloc, since no new states have yet been emitted. After that, we add the CounterIncrementPressed event to signal a change in state. Lastly, we close the internal state stream on the Bloc by printing the state of the Bloc, which has changed from 0 to 1.

####Stream Usage

Just like with Cubit, a Bloc is a special type of Stream, which means we can also subscribe to a Bloc for real-time updates to its state:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i39aqgcyqixjchnhcd7z.png)

Using the above snippet, we're subscribing to the CounterBloc and calling print on every change in state. We then add a CounterIncrementPressed event which triggers an on<CounterIncrementPressed>event handler and emits a new state. In the end, we call to cancel the subscription and close the bloc when we no longer require updates.

##Observing A Bloc

Bloc is an extension of BlocBase, which means we can observe all changes to a Bloc's state using onChange.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pi66k7b757olx6v6gupp.png)

Using main.dart, we can update it to:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9pwdxaas17sand7c48j.png)

Running the above snippet will produce the following output:

**Change { currentState: 0, nextState: 1 }**

We are able to capture information about what triggered a state change in Bloc because it is event-driven, which makes it unique from Cubit.

By overriding onTransition, we can accomplish this.

It is called a transition when one state changes to another. There are three types of transitions: the current state, the event, and the next state.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qtvz9b4w5xqbfr93fie.png)

We should now see the following output if we run the same main.dart snippet again:

**Transition { currentState: 0, event: Increment, nextState: 1 } Change { currentState: 0, nextState: 1 }**

###BlocObserver

In the same way as before, a custom [BlocObserver](https://bloclibrary.dev/#/coreconcepts?id=blocobserver-1) can override onTransition to watch all transitions in one place.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yquziocec6d748vas89j.png)

As before, we can initialize the SimpleBlocObserver as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8ng3z7ufqbt6lxagxda.png)

Upon running the above snippet, we should see the following output:

**Transition { currentState: 0, event: Increment, nextState: 1 } CounterBloc Transition { currentState: 0, event: Increment, nextState: 1 } Change { currentState: 0, nextState: 1 } CounterBloc Change { currentState: 0, nextState: 1 }**

The Bloc instance also provides us with the ability to override onEvent, which is called whenever a new event is added to the Bloc. The same goes for onChange and onTransition.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y48s75cmk40qvarkl80o.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7zepooci9ks1nf7tki8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8ixbg8ziaciervch97l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z96h3p8o2ucu8h6levrv.png)

Running main.dart again should produce the following result:

**Increment CounterBloc Increment Transition { currentState: 0, event: Increment, nextState: 1 } CounterBloc Transition { currentState: 0, event: Increment, nextState: 1 } Change { currentState: 0, nextState: 1 } CounterBloc Change { currentState: 0, nextState: 1 }**

##Flutter Bloc Tutorial For Timer

The following tutorials provide detailed instructions on how to build time, counter, login, weather, and a lot more apps using Flutter's Bloc library. These tutorials are worth checking out:

- [Counters](https://bloclibrary.dev/#/fluttercountertutorial)​ 
- [Timer](https://bloclibrary.dev/#/fluttertimertutorial)​ 
- [Infinite List​](https://bloclibrary.dev/#/flutterinfinitelisttutorial) 
- [Login](https://bloclibrary.dev/#/flutterlogintutorial)​ 
- [Weather](https://bloclibrary.dev/#/flutterweathertutorial)​ 
- [Todos](https://bloclibrary.dev/#/fluttertodostutorial)​ 
- [Firebase Login](https://bloclibrary.dev/#/flutterfirebaselogintutorial)​ 

_**Also, Read:** Flutter Tutorial for Beginners_​ 

##Conclusion

For further learning, we suggest reading the detailed documentation. In addition, don't forget to check out the example tutorials given in the blog. Using good state management is essential when building flutter applications.

To summarize, Flutter is great because it's understandable and not complicated to use. It also has a lot of management options for views or widgets.