Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask.
The hive_ce
package is a drop in replacement for Hive v2. Make the following replacements in your project:
pubspec.yaml
# old dependencies
dependencies:
hive: ^2.0.0
hive_flutter: ^1.0.0
dev_dependencies:
hive_generator: ^1.0.0
# new dependencies
dependencies:
hive_ce: latest
hive_ce_flutter: latest
dev_dependencies:
hive_ce_generator: latest
Dart files
// old imports
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
// new imports
import 'package:hive_ce/hive.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
- 🚀 Cross platform: mobile, desktop, browser
- ⚡ Great performance (see benchmark)
- ❤️ Simple, powerful, & intuitive API
- 🔒 Strong encryption built in
- 🎈 NO native dependencies
- 🔋 Batteries included
You can use Hive just like a map. It is not necessary to await Futures
.
import 'package:hive_ce/hive.dart';
void example() {
final box = Hive.box('myBox');
box.put('name', 'David');
final name = box.get('name');
print('Name: $name');
}
BoxCollections
are a set of boxes which can be similarly used as normal boxes, except of that
they dramatically improve speed on web. They support opening and closing all boxes of a collection
at once and more efficiently store data in indexed DB on web.
Aside, they also expose Transactions which can be used to speed up tremendous numbers of database transactions on web.
On dart:io
platforms, there is no performance gain by BoxCollections or Transactions. Only
BoxCollections might be useful for some box hierarchy and development experience.
import 'package:hive_ce/hive.dart';
import 'hive_cipher_impl.dart';
void example() async {
// Create a box collection
final collection = await BoxCollection.open(
// Name of your database
'MyFirstFluffyBox',
// Names of your boxes
{'cats', 'dogs'},
// Path where to store your boxes (Only used in Flutter / Dart IO)
path: './',
// Key to encrypt your boxes (Only used in Flutter / Dart IO)
key: HiveCipherImpl(),
);
// Open your boxes. Optional: Give it a type.
final catsBox = await collection.openBox<Map>('cats');
// Put something in
await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
await catsBox.put('loki', {'name': 'Loki', 'age': 2});
// Get values of type (immutable) Map?
final loki = await catsBox.get('loki');
print('Loki is ${loki?['age']} years old.');
// Returns a List of values
final cats = await catsBox.getAll(['loki', 'fluffy']);
print(cats);
// Returns a List<String> of all keys
final allCatKeys = await catsBox.getAllKeys();
print(allCatKeys);
// Returns a Map<String, Map> with all keys and entries
final catMap = await catsBox.getAllValues();
print(catMap);
// delete one or more entries
await catsBox.delete('loki');
await catsBox.deleteAll(['loki', 'fluffy']);
// ...or clear the whole box at once
await catsBox.clear();
// Speed up write actions with transactions
await collection.transaction(
() async {
await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
await catsBox.put('loki', {'name': 'Loki', 'age': 2});
// ...
},
boxNames: ['cats'], // By default all boxes become blocked.
readOnly: false,
);
}
Hive not only supports primitives, lists, and maps but also any Dart object you like. You need to generate type adapters before you can store custom objects.
Hive CE supports automatic type adapter generation using the GenerateAdapters
annotation. This new method of generation has the following benefits:
- No more manually adding annotations to every type and field
- Generate adapters for classes outside the current package
import 'package:hive_ce/hive.dart';
class Person extends HiveObject {
Person({required this.name, required this.age});
String name;
int age;
}
Usually this is placed in lib/hive/hive_adapters.dart
import 'package:hive_ce/hive.dart';
import 'person.dart';
part 'hive_adapters.g.dart';
@GenerateAdapters([AdapterSpec<Person>()])
// Annotations must be on some element
// ignore: unused_element
void _() {}
dev_dependencies:
build_runner: latest
hive_ce_generator: latest
dart pub run build_runner build --delete-conflicting-outputs
This will generate the following:
- TypeAdapters for the specified AdapterSpecs
- TypeAdapters for all explicitly defined HiveTypes
- A
hive_adapters.g.dart
file containing all adapters generated from theGenerateAdapters
annotation - A
hive_adapters.g.yaml
file - A
hive_registrar.g.dart
file containing an extension method to register all generated adapters
All of the generated files should be checked into version control. These files are explained in more detail below.
import 'dart:io';
import 'package:hive_ce/hive.dart';
import 'package:your_package/hive/hive_registrar.g.dart';
void main() {
Hive
..init(Directory.current.path)
..registerAdapters();
}
Extending HiveObject
is optional but it provides handy methods like save()
and delete()
.
import 'package:hive_ce/hive.dart';
import 'person.dart';
void example() async {
final box = await Hive.openBox('myBox');
final person = Person(name: 'Dave', age: 22);
await box.add(person);
print(box.getAt(0)); // Dave - 22
person.age = 30;
await person.save();
print(box.getAt(0)); // Dave - 30
}
The Hive schema is a generated yaml file that contains the information necessary to incrementally update the generated TypeAdapters as your model classes evolve.
Some migrations might require manual modifications to the Hive schema file. One example is field renaming. Without manual intervention, the generator will see both an added and removed field. To resolve this, manually rename the field in the schema.
Another example of manual modifications to the Hive schema is switching an existing app from explicit HiveTypes to the new GenerateAdapters
method. Take the following steps in this case:
- Make sure your existing TypeAdapters are up to date
- Convert any
HiveType.defaultValue
values to constructor parameter defaults - Follow the above instructions to set up a
GenerateAdapters
annotation for all your model classes. Type IDs will be generated according to the order of the classes in the annotation. - Make any necessary modifications to the Hive schema so that the new TypeAdapters match the old ones. Ensure that
nextTypeId
and thenextIndex
fields are correct. - Finally, remove all explicit
HiveType
andHiveField
annotations from your model classes
The old method of defining HiveTypes is still supported, but should be unnecessary now that Hive CE supports constructor parameter defaults. If you have a use-case that GenerateAdapters
does not support, please create an issue on GitHub.
Unfortunately it is not possible for GenerateAdapters
to handle private fields. You can use @protected
instead if necessary.
When adding a new non-nullable field to an existing object, you need to specify a default value to ensure compatibility with existing data.
For example, consider an existing database with a Person
object:
import 'package:hive_ce/hive.dart';
class Person extends HiveObject {
Person({required this.name, required this.age});
String name;
int age;
}
If you want to add a balance
field, you must specify a default value or else reading existing data will result in null errors:
import 'package:hive_ce/hive.dart';
class Person extends HiveObject {
Person({required this.name, required this.age, this.balance = 0});
String name;
int age;
double balance;
}
After modifying the model, remember to run build_runner
to regenerate the TypeAdapters
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 and initializing Hive, you can use Hive in your project:
import 'package:hive_ce/hive.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: Hive.box('settings').listenable(),
builder: (context, box, widget) {
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.
The benchmark was performed on a Oneplus 6T with Android Q. You can run the benchmark yourself.
*Take this benchmark with a grain of salt. It is very hard to compare databases objectively since they were made for different purposes.
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.