All writing

2 min read

Flutter Hive Tutorial

Hive, a fast key-value database written in pure Dart: set it up in Flutter, store data in boxes, and save your own objects with type adapters.

  • Flutter
  • Dart
  • Hive

First published on Medium (opens in a new tab)

1:50, read aloud by SONAR, word by word
The Hive logo

Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask.

Why do we need to use hive ?

  • 🚀 Cross platform: mobile, desktop, browser
  • ⚡ Great performance (see benchmark)
  • ❤️ Simple, powerful, & intuitive API
  • 🔒 Strong encryption built in

But the best thing is not necessary to await any thing (Futures)

and now let see how can we implement it

Dependencies

 hive: ^2.0.4
 hive_flutter: ^1.1.0
 hive_generator: ^1.1.0
 build_runner: ^2.0.5

How To Use

First of all you need to Initialize it

void main() async{
// Initializes Hive with a valid directory in your app files
await Hive.initFlutter();
runApp(MyApp());
}

And after that simply we can just open a box

// opening box like create a new table in sql
// it’s very important step
var box = await Hive.openBox(‘hive’);
// you can put data like map in this form
box.put(‘name’, ‘Yazan’);
// you can store list too
box.put(‘numbers’, [‘1’,’2',’3']);
and now you wonder how we can get the data form the box and where
// whatever the place and time just call the box you want
var box = Hive.box(‘hive’);
// by providing the key you want you will find your answer
print(box.get(‘numbers’));
// output : [1, 2, 3]
// note : Hive supports all primitive types, List, Map, DateTime, BigInt and Uint8List

Now what if we want to store an object

First of all we need to register our new object in hive by using Type Adapters .

No need to do the hard work by yourself you can easily use hive generator to generate the adapter so the steps we must do are :

1- Define the object you want to register

import ‘package:hive/hive.dart’;
// note : part is very important to define to let the build runner do his work
// you will face an error don’t be afraid the build runner will fix this
part ‘person.g.dart’;
// HiveType annotation for generate the type adapter
// hive type id is between 0–223 and it’s for indexing the object
@HiveType(typeId: 0)
class Person extends HiveObject {
// Annotate all fields which should be stored with HiveField
@HiveField(0)
 String? name;
@HiveField(1)
 int? age;
Person({this.name,this.age});
}

2 — Run build task flutter packages pub run build_runner build and the build runner will generate file contain the type adapter for our defined class in previous step (Person)

The generated person.g.dart file next to person.dart, opened on class PersonAdapter extends TypeAdapter<Person>

3 — And finally we just need to register our new adapter like this

Hive.registerAdapter(PersonAdapter());

and now you can put and get this object in hive like this

Person person = Person(name: ‘Yazan’,age: 21);
box.put(‘customObject’, person);
print(‘my name is ${person.name} and my age is ${person.age}’);
// output : my name is Yazan and my age is 21
// Bonus : you can put List<Person> too
box.put(‘employees’,[Person(name: ‘Yazan’),Person(name:’Ayham’)]);
print(box.get(‘employees’));
// output : [Instance of ‘Person’, Instance of ‘Person’] :)

Note 1 : don’t forget define box before read or write and if you have an error usually you didn’t open the box .

Note 2 : you can write and read wherever you want if the box exist or opened before

Thanks you for reading I will drop for you this links :