description |
---|
Lightweight and blazing fast key-value database written in pure Dart. |
Add the following to your pubspec.yaml
. Use the latest version instead of [version]
.
dependencies:
hive: ^[version]
hive_flutter: ^[version]
dev_dependencies:
hive_generator: ^[version]
build_runner: ^[version]
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 has its 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()
.
All of your data is stored in boxes.
var box = await Hive.openBox('testBox');
{% hint style="info" %}
You may call box('testBox')
to get the singleton instance of an already opened box. This will save one async call.
{% endhint %}
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');
{% embed url="https://www.youtube.com/watch?v=R1GSrrItqUs" caption="Learn the basics of using Hive in this well-made tutorial by Reso Coder." %}