Photo by Silvie Lindemann from Pexels

How to use pre-populated hive database on flutter apps

This problem come to me recently. We need to ship Flutter apps with preload or pre-populated database. Pre-populated database it means that you need to ship the apps with database include in the first time. There is another way you can use some preload data, using json, csv, or another database format for example the most popular is SQLite. But in apps that I’m working right now we using NoSQL database to save the data.

Hive, is one of the NoSQL databases that available for Flutter. There is some more actually like objectBox or sembast. Why we using Hive? Because one of the good things using it because the speed both for writing or read to the database. Aside from that, Hive also very lightweight. Because it’s NoSQL type of database, we do not need care about relations because it’s a key value based.

So, how to pre-populated the database using Hive databases. In Hive, the database is saved in things called “box”. If the database is SQLite, you copy that database into assets folder. Just don’t forget to register it on pubspec.yaml. After that you can use the database helper to read from the assets file to use immediately in your app. If you using the SQLite you don’t have to worry, because there is already a lot example out there.

The problem I encounter with Hive is, I cannot find the box database that already generated. We need to ship the database so the user can use it after the apps get installed. Well, finding the box is a little bit challenge, because right now there is less example how to pre-populated with hive. So how I found it? I went to official GitHub for hive to see if there is an issue related to this problem. Thankfully I, found it but it’s just half of it. Don’t worry, I’ll explain to you below.

How to load pre-populated hive database from assets Flutter


  • Android Studio
  • Path provider package

Here’s the things that we need to follow. The first one, how to locate the hive box that already created? To find the box you can use Android Studio device file explorer. To use it go to view> tool windows > device file explorer.  Don’t forget to connect the Android Studio with emulator or your smartphone.

I’ve been created Flutter project with the name “hive_copy_box”. So, the file directory name in device file explorer will be “com.hive_copy_box”. From the explorer menu go to directory data > data > your project > app_flutter name in this case will be directory name “com.hive_copy_box”. You can find the example below.

Device Explorer Android Studio
Device Explorer Android Studio

After you find the box, right click and choose save as in another folder as your desired in within your computer. The next step is to copy and register the hive box in pubspec.yaml. To copy the box into the application system you need to user path_provider package.

Code how to load pre-populated box from

Example load hive data from assets Flutter
Example load hive data from assets Flutter

The next step is, I will explain the code.  Make sure called the Widgets Binding before on your main app. After that, access your app system directory using the path_provider to create your box. Create your new hive box in this case “boxcopy”. The next important things, open your new box and do not for forget to close it first before transfer the data from the assets.

The last step is to transfer from data from the asset to the new box in your application system. Congratulation, you’re done. Now you can use the data from the new box in your application system. That’s all about how to pre-populate database using hive in Flutter apps. Hope it can help for people that need to use pre-populated using hive database in Flutter.


Leave a Reply

Your email address will not be published. Required fields are marked *