dartnative_hive
Hive for DartNative: the key-value database written in Dart, with writes many times faster than Hive in a Flutter app, and the same API on iOS and Android.
Why you'll like it
- Very fast writes: on an iPhone, 13 to 57 times faster than Hive in a Flutter app on Hive's own benchmark. A small value takes under a microsecond.
- Safe writes: boxes are stored in memory-mapped files, a mechanism built into iOS and Android, so nothing is lost if the app crashes or is killed.
- The Hive you know: boxes, type adapters, lazy boxes, encrypted boxes, box events and keys in sorted order work as they do in any Hive app.
Highlights
Hive.initDartNative(documentsPath, {subDir, mmap}): point Hive at a storage directory.Hive.openBox(name)andbox.put/box.get/box.delete: the usual Hive API.
Install
dependencies:
dartnative_hive: ^2.0.0 # from dartpub.dev
dn pub get
Quick look
import 'package:dartnative_hive/dartnative_hive.dart';
// Point Hive at your app's documents directory, once at startup
// (getApplicationDocumentsDirectory() from dartnative_path_provider):
Hive.initDartNative(documentsPath);
final box = await Hive.openBox('settings');
await box.put('theme', 'dark');
final theme = box.get('theme'); // 'dark'
Type adapters
To store your own classes and enums, generate their adapters with dartnative_hive_generator:
dev_dependencies:
dartnative_hive_generator:
hosted: https://dartpub.dev
version: ^2.0.0
build_runner: ^2.5.4
dn pub run build_runner build
Its README shows the annotations. Adapters written by hand work as they do in any Hive app.
Platform setup
iOS
No native setup required.
Android
No native setup required.
Fast, safe writes
Boxes are stored in memory-mapped files, a mechanism built into iOS and Android: the file's
contents sit in memory that belongs to the operating system, not to the app, and the system
writes them to storage itself. A put copies the value there before it returns, even if its
future is never awaited. From that moment the value is safe: nothing is lost if the app
crashes, is closed from the app switcher, or is stopped by the system. A small value takes
under a microsecond.
The one event the system cannot cover is a sudden loss of power before it has written the
data to storage, which can lose the latest writes of any app that writes files. When a value
must survive even that, await box.flush(): it returns once the data is on the device's
storage.
Reads are a lookup in memory.
On Hive's own benchmark, 1,000 awaited box.add calls of a ten-field object take
1.61 ms on an iPhone 16 Pro, against 22.8 ms for Hive in a Flutter app on the same
phone; 100,000 take 0.17 s against 2.34 s.

Writes and reads of a short string, on 1 key and on 1000 keys, on the same phone:

A mid-range Android phone shows gains of the same size, measured against Hive's standard file storage on that phone: 16 to 97 times faster on Hive's benchmark, and 23 to 49 times faster on awaited writes of a short string.
The example's
benchmarks/ folder has every number and how it was measured.
To use Hive's standard file storage instead, where a put is safe once its future
completes, pass mmap: false:
Hive.initDartNative(documentsPath, mmap: false);
The file format is the same either way, so existing boxes open in both modes. The example's Benchmark screen times writes and reads in both modes on your own device.
Example
The example/ app opens a box and round-trips a value, and its Benchmark
screen times every kind of write and read in both modes. Borrow from it freely.
Run it:
cd example
dn pub get
dn run -d <device-id>
The example is an official demo: free to run, no license setup. Always use dn (not the
underlying SDK CLI) for run/build/pub commands.
Credits & license
Built on the source code of Hive's community edition
(hive_ce); its licence is in THIRD_PARTY_NOTICES.
First-party DartNative plugin, free to use in any DartNative app. Distributed through dartpub.dev; report issues on the plugin's page.