Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

flutter
hive
communitycreator

What are the basics of Hive in Flutter?

Wojtek

Managing data and storing it locally on a device in a Flutter app can be quite challenging. Fortunately, with Hivea key-value database written in pure Dart, we can manage our local database with ease!

What exactly is Hive?

According to Hive’s documentation:
“Hive is a lightweight and blazing fast key-value database written in pure Dart.”

With Hive, we can save application data on a device and get it extremely fast whenever we need it.

Performance

Hive is much faster than most other popular local storage alternatives.

1000 read iterations
1000 write iterations

Usage

Using Hive in a Flutter project is a bit different than using it in a pure Dart application. The only difference is in setup, which we’ll cover in this shot.

How to import the Hive package

First, we need to import the package. We’ll use the command below in the terminal:

flutter pub add hive
Importing package

We can also manually import the Hive package to pubspec.yaml:

dependencies:
  hive: ^2.0.4
Manually import package

Note: The current version of Hive is 2.0.4. If you want the latest version, check it out on pub.dev.

To use Hive with Flutter, we also need to import the hive_flutter package with the following command:

flutter pub add hive_flutter
Importing hive_flutter package

We can also manually import it to pubspec.yaml:

dependencies:
  hive_flutter: ^1.1.0
Manually importing hive_flutter package

Setup

For Hive to work with Flutter, we need to set up some things first.

In the main.dart file, we need to change some things in the main method. When we create a new Flutter project, it looks as follows:

void main() {
  runApp(MyApp());
}
New Flutter project

For Hive to work, it must look something like the following code:

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

void main() async {
  // Allows for async code in main method
  WidgetsFlutterBinding.ensureInitialized();
  // Initialises Hive
  await Hive.initFlutter();
  runApp(MyApp());
}
Hive working in the main.dart file

The additional lines of code in the main method make sure that Hive is initialized before our application is run.

Boxes

Hive uses boxes as a place where the data is stored. Let’s think of the box as a container for data of a specific area of our application.

For example, our application needs to store user data and settings. In that case, two boxes named userBox and settingsBox should be more than enough.

How to open the box

To open a box, use the following command:

await Hive.openBox('boxName');
Opening the box

We’ll replace boxName with the name we want to give to our box.

The example with user and settings will look as follows:

await Hive.openBox('userBox');
await Hive.openBox('settingsBox');
User and Settings example

We need to open a box before we write to or read data from it. We’ll open a box just after we initialize Hive.

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  // Opens a new box called 'myBox'
  await Hive.openBox('myBox');
  runApp(MyApp());
}
Opening a box after initializing Hive

How to access the box

To perform actions on the data in a box, we need to access it first, as shown below:

final box = Hive.box('boxName');
Accessing the box

Let’s make sure that a box with that name has already been opened.

How to write data to a box

To write data to a box, we use the put method:

box.put('key', 'value');
Writing to a box

Writing data to a box is similar to writing data to a map. We store values under specified keys. Note that a value can be a number, list, or any other Dart type or object.

box.put('myString', 'Hello World!');
box.put('myNumber', 7);
box.put('myList', []);
Writing different values to a box

How to read data from a box

To read data from a box, we use the get method:

final value = box.get('key');
Reading data

If the key does not exist, the method returns null. To avoid this, we can specify an optional defaultValue parameter.

final value = box.get('nonexistingKey', , defaultValue: 'myDefaultValue');
defaultValue parameter specified

How to delete key-value pairs

Sometimes we want to delete data that we don’t need anymore. We can use the delete method, which accepts a specified key:

box.delete('myKey');
Deleting a pair

Conclusion

Managing our local database with Hive is super easy. If you want to learn more about Hive, please check out the official documentation.

RELATED TAGS

flutter
hive
communitycreator
RELATED COURSES

View all Courses

Keep Exploring