Skip to main content

Flutter Hive — The complete crash course

Flutter Hive — The complete crash course

Introduction

Hive is a great package for your local storage. But sometimes it’s quite complex to get into that package. That’s why, in this article, I will show you how to use Hive very easily. Let’s get started!

Note: You can view the whole source code used in this article here.

Why should I learn Hive?

You may be thinking, that this is obvious: Because it’s a great local database solution. Well, in comparison to some packages, it clearly is the case, but there is one package that is superior in every way. It’s called Isar and was written by the same author. I’ve even started a tutorial series on that (More articles will follow soon).

Okay, now this sounds very demotivating, but Hive is still very useful. Hive is still used and preferred by some companies, that like the way you interact with Hive and don’t need all the “fancy” stuff that Isar can do. And you maybe don’t need it either. In addition, it’s literally so easy to use after you have understood it, you will never want to miss the simplicity again. So, let us learn Hive, it only takes a few minutes to learn and you have a new skill!

Installation

The first thing we have to do after creating our app is to add hive. To do so, we will use the commands flutter pub add hive, flutter pub add hive_flutter (If you want to use Hive with Flutter), flutter pub add hive_generator --dev and flutter pub add build_runner --dev. You should be familiar with those commands, otherwise, I recommend you to look up the basics of Flutter again.

Now we want to initialize Hive. To do so, we call await Hive.initFlutter(), or, if we want to use Hive in a non-flutter app, we can call Hive.init().

How Hive works

Hive stores data in “boxes”. But what are boxes? Look at one box as a SQL table, but has no structure and can contain anything. One box is maybe enough for smaller apps (Todo List app etc.) but for bigger apps multiple boxes are very important to structure everything.

Boxes have many features, for example, they can encrypt data to store sensitive data. In addition, when your app is killed during a write operation, the item might be corrupted. This will be deleted as soon as the app starts again. You can disable this of course. There are many other features, like providing the box in binary form, setting its own rules for automatic compaction, and providing a custom sorting order.

The basics of boxes

In Hive, if we want to use a box, we first have to open it first:

Because Hive stores a reference to all opened boxes, you can call Hive.box(myBox). But why do I need this? Well simple question, simple answer: You don’t have to pass the boxes between the Widgets. That’s pretty neat, right?

Okay, but now we want to add an entry to our box. Every entry in a box has a key and a value. You can set one value at once with box.put(key, value) or add multiple values add once with box.putAll({key1: value1, key2: value2}). Okay, so let’s see it in action:

But it wouldn’t make any sense to store values if you can’t get them, right?

Okay, so let’s get some entries. To do this, we simply call box.get(key). Let’s say we want to get the value of my_key_1:

Now we want to delete my_key_2. And who would have thought it, you can do it with box.delete(key). Let’s see it in action:

Filtering items

This is a very short chapter but in my opinion quite important for most of you. Assuming we have a box called userBox which can only take a value of a User object (name, email, phone). Now we want to get every user where the email ends with @gmail.com. Let’s do this:

As you can see, it’s also very easy to filter items.

Closing Hive

It’s also possible to close a box if you don’t need it anymore. It’s not a problem if you leave it open during runtime of the app, but it’s generally a good practice to close it (if you don’t need it anymore in the future). In addition, before your whole application exits, call Hive.close() to be sure everything is fine. You also don’t have to worry here, but as said already, it’s a good practice to do so.

Further reading and conclusion

In this article, you have learned the basics of the local database solution “Hive”. You have seen how easy it is to use and store data locally.

You can unfold the whole power of Hive if you use packages like Freezed, Formz, or Flutter Hooks.

If you want to learn these additions, I have whole tutorials about them. Check them out here.

In the next few articles, I will introduce more somewhat complicated packages and explain them (e.g. Dio). If you don’t want to miss this, I recommend you to follow me.

I tried my best to write the easiest tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!