Generate adapter

The hive_generator automatically generates TypeAdapters for almost any class.

  1. To generate a TypeAdapter for a class, annotate it with @HiveType

  2. Annotate all fields which should be stored with @HiveField

  3. Run build task flutter packages pub run build_runner build

  4. Register adapter

Example

Given a library person.dart with an Person class annotated with @HiveType:

import 'package:hive/hive.dart';
part 'person.g.dart';
()
class Person {
(0)
String name;
(1)
int age;
(2)
List<Person> friends;
}

As you can see, each field has a unique number (unique per class). These field numbers are used to identify the fields in the Hive binary format, and should not be changed once your class is in use.

Field numbers can be in the range 0-255.

The above code will generate an adapter class called PersonAdapter. You can change that name with the optional adapterName parameter of @HiveType.

Updating a class

If an existing class needs to be changed – for example, you'd like the class to have an extra field – but you'd still like to read objects written with the old adapter, don't worry! It's very simple to update generated adapters without breaking any of your existing code. Just remember the following rules:

  • Don't change the field numbers for any existing fields.

  • If you add new fields, any objects written by the "old" adapter can still be read by the new adapter. These fields will just be ignored. Similarly, objects written by your new code can be read by your old code: the new field will be ignored when parsing.

  • Fields can be renamed and even changed from public to private or vice versa as long as the field number stays the same.

  • Fields can be removed, as long as the field number is not used again in your updated class.

  • Changing the type of a field is not supported. You should create a new one instead.

Enums

Generating an adapter for enums works almost like it does for classes:

()
enum HairColor {
(0)
brown,
(1)
blond,
(2)
black,
}

For updating the enum the same rules apply as above.