Caching user data like a pro in flutter

Josephisiyemi
3 min readMar 1, 2021

Using Hive to cache user data in flutter

Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask.

Hive is a blazing fast NoSQL database that stores data in keys and value pairs, which is further saved in boxes.

We will be talking about using hive database with a specific use case in this tutorial.

Other options

Caching and persisting user data is a very important part of all mobile apps, because one way or the other you have to stash some form of user data for later use.Depending on your use case Shared Preferences and Sqflite could also be used and will work perfectly.

What if the data you want to store isin’t complex or large enough to need an sqflite database? Take a look at the performance benchmark of Hive when compared with other storage option.

Hive vs SharedPreference vs Sqlite

Hive boast of better performance than shared preference in write operations, coupled with the fact that hive more safe since each box has a unique Key.

Caution

If your use case requires you carry complex operations on your data such as storing large amount of data, Storing blobs or using triggers then it’s advisable you use Sqflite. Other than that hive is your guy.

Let’s see how Hive works

  1. Hive stores data in a box, a box is like a regular treasure box where you store your treasure, only that this time your user’s data is the treasure.

2. Each box has a key without which you cannot open the box

Without the key you can’t open an hive box or have access to the data contained in the box

How to cache data with hive

1. Open your Hive box like this

initHive() async {   try {      print("Tried opening box");      box = await Hive.openBox(userInfo.user_details.id.toString());   } catch (e) {       print("Error");       } }

You should do this as early as possible in your app, if you are a little confused on where to put it, here is a little guide:

Take note of the global variable box at the top.

Make sure you open your box before trying to access it or else you will end up with an error.

2. Saving data to the box

Saving data to your hive box is ver very easy, don’t believe? take a look

box.put("USER_NAME", "Joseph Isiyemi");

Remember we create a global variable box? so we can access that variable from anywhere in your app, now all we have to do is call .put(key , value) on our box and we are fine.

3. Retrieving data from our box

Retrieving data from the box is just as easy as adding value to the box

String userName  = box.get("USER_NAME"); // Joseph Isiyemi

and there you go, your user’s data has been retrieved successfully.

Its that easy to store and fetch data from a Hive box.

Advantages of using Hive

  1. There can be any issue of bridging user data when another user logs in on the same device
  2. You can store data easily and fast
  3. You can save Object to Hive using TypeAdapter

Thanks for taking time to go through my story, hope you learnt a thing or two, kindly tell me what you think in the comment section and don’t forget to give me a clap.

--

--

Josephisiyemi

Mobile Developer with years of Extensive mobile ( Android & Hybrid) application development experience with strong expertise in Kotlin and flutter