Relationships in Hive — Flutter

Bittu Patel
2 min readApr 12, 2021

NOT ALL HUMANS HAVE RELATIONSHIP, BUT DATABASES DO

Introduction

Today we continue our journey into the world of the Hive database. In this tutorial of the series, we will learn how to work with multiple objects that have relationships with each other.

HiveLists

HiveLists provide a simple way to solve the problem. They allow you to store a “link” to the actual object.

Let’s understand this with the example of a ToDo app, where we create tasks and assign them to a specific user.

Create a TypeAdapter for Task object

First, we create a Task object using HiveObject. Then we create a TypeAdapter for the Task object and define read and write methods.

Create a TypeAdapter for User object

First, we create a User object using HiveObject, as we did for the task. Then we create a TypeAdapter for the User model and define read and write methods.

Register Adapters and Open Boxes

We register both the TaskAdapter and the UserAdapter. Then we open a box to save tasks and users respectively.

Add to Boxes

We add a new user to the users box. Then we add a new task to the tasks box.

Assign Task to User

Now we create a HiveList that contains the user associated with the task. The HiveList constructor needs the HiveObject that will contain the list. The list must not be moved to another HiveObject. If we delete an object from a box, it will also be deleted from all HiveLists. However, if we delete an object from a HiveList, it will remain in the box.

Source Code

Below you can find the final code.

The End

Congratulations, you have finished this tutorial where you learned how to create relationships between objects using HiveList.

Do you want to learn more about Hive?

Let us know in the comments below.

www.developerb2.com

--

--

Bittu Patel

Expertise in Frontend, Backend, and Everything in Between