Cache API Integration with HydratedBLoC in Flutter (Source Codes Included)
Tags: flutter, api, bloc, cache
Published: 2021-10-27 19:25:15

#Introduction 🎉
<br />

BLoC stands for Business Logic Controller. It was created by Google and introduced at Google I/O 2018. It is made based on Streams and Reactive Programming.
<br />

I can assure you that every intermediate-level Flutter developer in their development lifetime heard about Bloc or maybe tried to learn bloc. Bloc is one of the most popular state management choices among developers because it has rich documentation and is well maintained. But yes, there are some downsides as well, for example, a lot of boilerplate codes.
<br />

We will implement API integration at first, then persist the state so that when the user closes the app, it can maintain the state or load the data from the local device saved from the last API call to put it simply.
<br />

Note that after we kill the app, it starts right back where it left off. In addition, after it has loaded the previous (cached) state, the app requests the latest API data and updates seamlessly. Let’s get started!
<br />

<br />
![I won](https://miro.medium.com/max/500/0*OOohPMPoLAgC_hXS)
<br />

#Steps 👣
####1. Configuring the Flutter Project
####2. Add the datamodel
####3. Creating the bloc
####4. Creating the bloc state and event
####5. Creating the Bloc Repository
####6. Implementing the bloc
<br />

<br />
![I won](https://miro.medium.com/max/320/0*9wL0qaE3oOUUd0Nl.gif)
<br />


#1. Configuring the Flutter project ⚙️
Let’s add the necessary packages that we’re going to use throughout the application.

Copy the dependencies to your Pubspec.yaml file. I am using the latest version available now at this moment.

{% gist https://gist.github.com/ImranSefat/417eb1acf77716cb6d79df140b699c8c %}



Then we need to install it with:
```
flutter packages get
```

You will get to understand everything as we go ahead.


<br />
![Datamodel](https://miro.medium.com/max/415/0*BB4B-IgNEWa0TJvb)
<br />


#2. Add the datamodel 📳
We will implement the “FREETOGAME API”. For this, we have to make a datamodel of the API’s response. I have used the following website to make the datamodel class. It’s pretty easy, copy the JSON response and paste it on the website. The website will generate a class for you.
<br />

> Website Link: https://ashamp.github.io/jsonToDartModel/
> Don’t forget to tick the Null Safety checkbox!
<br />

{% gist https://gist.github.com/ImranSefat/a95d9f3eb4ad8620db497d35cb3a23d9 %}

<br />

Another datamodel that will contain the list of the games refers to the code below.
<br />
{% gist https://gist.github.com/ImranSefat/0a6a5e766dc9df5d85d70e4455a53c5e %}
<br />

The above code will show you some errors, as you can see that the 5th line contains a code that indicates that this file is part of another file that needs to be generated. Another thing, look at the 7th line, it is indicating that we will serialize so that we can save the response for later use.
<br/>
Open a terminal and run the below code.
> flutter packages pub run build_runner build

<br />
![bloc](https://miro.medium.com/max/320/0*gBkU4Qpdn7UjipHp)
<br />

#3. Creating the bloc
<br />

{% gist https://gist.github.com/ImranSefat/e8031cdd341726688ea89d00cb755df2 %}
<br />
It contains the logic behind the main bloc. Now we have to make the event and state as well.

<br />
![bloc](https://miro.medium.com/max/352/0*7-mIGJ58Wj7HPjwm)
<br />

#4. Creating the bloc state and event

{% gist https://gist.github.com/ImranSefat/e8031cdd341726688ea89d00cb755df2 %}
<br />

There can be three(3) states.
1. Game list is loading -> GamelistLoading
2. Game list loaded -> GamelistLoaded
3. Game list cannot be loaded -> GamelistError

<br />
![loading](https://miro.medium.com/max/480/0*oRzNrsG2ehRBKYEm.gif)
<br />

#5. Creating the Bloc Repository👾
{% gist https://gist.github.com/ImranSefat/f01ffbb344d00da417eb490cc67dab02 %}


We are calling the API using the HTTP package form from this file or class.

<br />
![loading](https://miro.medium.com/max/480/0*mmOy98cWIGqgpjPd.gif)
<br />

#6. Implementing the bloc🛠
{% gist https://gist.github.com/ImranSefat/2902f72681f51fc91bd6bbbbaee40d18 %}
<br/>
This portion contains the UI and the Bloc implementation. You can check the main function. It is instantiating the hydrated bloc in the temporary directory.
Note that after we kill the app it starts right back where it left off. In addition, after it has loaded the previous (cached) state, the app requests the latest API data and updates seamlessly.
<br />
<br />

#Congratulations! 🎊
You just integrated an API with HydratedBloc that has Caching.

<br />
![success](
https://miro.medium.com/max/480/0*s1NXqYGxJqv9Tk9x.gif)
<br />

<br />
<br />
#Contact Me🌎
YouTube Channel: [Coding with Imran](https://www.youtube.com/c/CodingwithImran)
Twitter: [@ImranSefat](https://twitter.com/ImranSefat)
LinkedIn: [MD. Al Imran Sefat](https://www.linkedin.com/in/imransefat/)
Facebook Page: [Coding with Imran](https://www.facebook.com/CodingWithImran)