Hive Documentation

Lightweight and blazing fast key-value database written in pure Dart.

Add Hive to project

Add the following to your pubspec.yaml. Use the latest version instead of [version].

Core version Generator version Flutter version Build runner version

dependencies:
hive: ^[version]
hive_flutter: ^[version]
dev_dependencies:
hive_generator: ^[version]
build_runner: ^[version]

Initialize

Before you can use Hive in your app, you must initialize it. This only has to be done once.

Give Hive a directory where it can store its files. It is recommended to use an empty directory. Each box will have it's own .hive file in the Hive-home directory.

Hive.init('path/to/hive');

If you use a directory outside your app files, make sure to request runtime permission on Android.

In the browser you don't have to call Hive.init().

Open a Box

All of your data is stored in boxes.

var box = await Hive.openBox('testBox');

You may call box('testBox') to get the singleton instance of an already opened box. This will save one async call.

Read & Write

Hive supports all primitive types, List, Map, DateTime and Uint8List. Any object can be can stored using TypeAdapters.

box.put('name', 'David');
var name = box.get('name');
print('Name: $name');

Video Tutorial

Learn the basics of using Hive in this well made tutorial by Reso Coder.