hive 1.2.0 hive: ^1.2.0 copied to clipboard
Lightweight and blazing fast key-value database written in pure Dart. Strongly encrypted using AES-256.
Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask.
- Documentation π
- Frequently Asked Questions π
- Samples π₯
Flutter Web Demos πΈοΈ #
Features #
Cross-platform β‘ #
- Runs on desktop, mobile & in browser
- Very good performance (see benchmark)
Easy to use β€οΈ #
- Keys are of type String or uint32 and values are arbitrary objects
- The basic operations are
put(key, value)
,get(key)
,delete(key)
- Strong encryption built in
Lightweight π #
- Small runtime
- Small disk space consumption
- NO native dependencies
Getting started #
To get started using Hive in a Flutter project, add the following dependencies 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]
Usage #
You can use Hive just like a map. It is not necessary to await Future
s.
Hive.init(Directory.current.path);
var box = await Hive.openBox('myBox');
box.put('name', 'David');
var name = box.get('name');
print('Name: $name');
Store objects #
Hive not only supports primitives, lists and maps but also any Dart object you like. You need to generate a type adapter before you can store objects.
@HiveType()
class Person extends HiveObject {
@HiveField(0)
String name;
@HiveField(1)
int age;
}
Extending HiveObject
is optional but it provides handy methods like save()
and delete()
.
Hive.init(Directory.current.path);
var box = await Hive.openBox('myBox');
var person = Person()
..name = 'Dave'
..age = 22;
box.add(person);
print(box.getAt(0)); // Dave - 22
person.age = 30;
person.save();
print(box.getAt(0)) // Dave - 30
Hive β€οΈ Flutter #
Hive was written with Flutter in mind. It is a perfect fit if you need a lightweight datastore for your app. After adding the required dependencies to your pubspec.yaml
, you are able to use Hive in your project:
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return WatchBoxBuilder(
box: Hive.box('settings'),
builder: (context, box) {
return Switch(
value: box.get('darkMode'),
onChanged: (val) {
box.put('darkMode', val);
}
)
},
);
}
}
Boxes are cached and therefore fast enough to be used directly in the build()
method of Flutter widgets.
Benchmark #
Read 1000 entries | Write 1000 entries |
---|---|
SharedPreferences is on par with Hive when it comes to read performance. SQLite performs much worse. | Hive greatly outperforms SQLite and SharedPreferences when it comes to writing or deleting. |
This benchmark was performed on a Oneplus 6T with Android Q. All entries are read and written one after another. You can run the benchmark yourself.
Take this benchmark with a grain of salt it is very hard to compare databases objectively since they werde made for different putposes.
Todo #
The work on Hive has just started. If you want to contribute, it would be amazing if you helped me with one of these:
- β Good test coverage
- β Many examples, especially for Flutter
- β Benchmarks and comparison
- β Finalize API
- β Even more tests
- β Queries
- β Improve documentation
- β Write binary format spec
- β You can never have enough tests
Licence #
Copyright 2019 Simon Leier
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.