Flutter with Hive Database

Abhishek Dixit
GYTWorkz
Published in
3 min readJun 29, 2022

What is Flutter?

Flutter is an open source framework by Google for building beautiful, natively compiled, multi-platform applications from a single codebase.

What is Hive?

Hive is a pure Dart-based, lightweight, and lightning-quick key-value database.

What is Box?

All data stored in Hive is organised in boxes. A box can be compared to a table in SQL, but it does not have a structure and can contain anything.

For a small app, a single box might be enough. For more advanced problems, boxes are a great way to organise your data. Boxes can also be encrypted to store sensitive data.

Add dependencies to your project

In your project go to pubspec.yaml and look for dependencies. Under dependencies, add the latest version of hive , hive_flutter and path_provider.

NOTE : We use the path_provider package to get the commonly used location such as TemporaryDirectory and ApplicationDocumentsDirectory.

  1. Create Singleton Class

We assure that there is only one class instance and that it has global point access by using the singleton technique.

Here, In the getDatabaseInstance method, we initialise the Box for Hive and configure its path.

2. Configure the Database Path , initialise Box and Get Box

Now, Let’s setup the database path

Here, i have extended the HiveInterface and configure the path as given below

Now, let’s initialise Box of the Hive

after that if there is data in the box of the hive then add to the stream so that we can show in the UI

3. Create Model Classes

For this Project, I’will be storing the name and age of the user. So i have made a model person for the storing those data

Apart from that, i’ll be returning the list of data with the bool value which represents the initialisation of the database

4. Insert, Read And Delete Operations

Here, we have written multiple methods for different operations

let’s understand one by one,

insert method :

we are storing the data using the put method of box class in the form of key, value pair like

e.g : {"uid":{"username":"age"}}

read method :

we are reading the data using the box class in the form of list of key, value pair

e.g : [{"uid1":{"username1":"age1"}},{"uid2":{"username2":"age2"}}]

delete method :

In order to delete the data in the box, we will be using the key.

clear method :

we are deleting all the data from the box and closing the stream controller.

Note :

  1. For every method, we are checking if the Box is not initialised, then we initialised first then we perform the operation.
  2. We are adding the data to the sink for the insert and delete methods so that the user interface will be updated.

5. UI Implementation

In the initState Method, we are initialising the the database and and creating the new stream controller

In the dispose Method, we are clearing the data of the box and closing the stream controller

In the build Method, we will be using the StreamBuilder widget for listening to the changes in the stream and ConnectionState

6. Demo

This is the result of the hard work we have been doing till now.

For more detail, checkout my Github repository

click here

Don’t forget to connect with me on:

--

--