Flutter & Hive Database: CRUD Tutorial

Flutter & Hive Database: CRUD Tutorial

Hive is a lightweight key-value database that may be used in mobile, desktop, and web applications to store data locally. It’s written entirely in Dart and integrates seamlessly with Flutter. The four main operations of persistent storage are CRUD (create, read, update, and delete).
In this article, we’ll utilize Hive and Flutter to create a full-featured and really useful app.

Getting Started With Hive Database in Flutter

Overview

Some of Hive’s benefits:

  • Like Dart maps, it’s simple to use. To query data, you don’t need to construct long and sophisticated queries.
  • High degree of adaptability. Because Hive does not employ tables like SQLite, you can easily change your data structure.
  • There are no native dependencies.
  • It’s quick and can manage a lot of data.
  • Dart objects and primitives (string, number, list, map, etc.) are supported (with the help of adapters)

You’ll need to install both the hive and hive flutter plugins in order to use Hive in Flutter.

You can initialize Hive in your app’s main() function:

void main() async { /*... */ await Hive.initFlutter(); /*...*/ }
Code language: JavaScript (javascript)

Hive organizes all of its data into boxes. In SQL, a box is similar to a table, but it lacks structure and can hold anything. You can open a box in the following manner:

await Hive.openBox('shopping_box'); // the name is totally up to you
Code language: JavaScript (javascript)

When you open a box, all of its data from local storage is put into memory for instant access. Without utilizing async/await, you can retrieve data synchronously:

final myBox = Hive.box('shopping_box'); final something = myBox.get('my_key');
Code language: PHP (php)

To add a new item to the box, follow these steps:

await myBox.put('some_key', someValue);
Code language: JavaScript (javascript)

Adding a new item to an existing box:

await myBox.put('some_key', someValue);
Code language: JavaScript (javascript)

Delete an item from a box:

await myBox.delete('some_key');
Code language: JavaScript (javascript)

Storing a list of items with auto-increment keys in Hive

Using Hive’s add() method, you may quickly save a list of items. The keys are auto-generated and auto-increment in this example, starting at 0, then 1, 2, 3, and so on. Consider the following scenario:

final listBox = Hive.box('my_list'); int newKey = await listBox.add(someData);
Code language: JavaScript (javascript)

You can get all the keys and values from the box by doing the following:

final keys = listBox.keys; final values = listBox.values;
Code language: PHP (php)

As appropriate, you can employ loops with keys and values. If you prefer to construct and set your own keys (as indicated earlier using the put() technique), DateTime.now is a good option (). It’s a good idea to use toString().

Conclusion

In this article, you’ve learnt the basics of the Hive database. Hive is a powerful package that makes it easy for developers to store data offline. As always, If you have found this article useful do not forget to share it and leave a comment if you have any questions. Happy Coding 🙂

%d bloggers like this: