Hive introduction
You can find everything here https://pub.dev/packages/hive
Steps
From Visual Studio Code
Cmd + Shift + P
toAdd dependency
then search forhive_flutter
.From Visual Studio Code
Cmd + Shift + P
toAdd Dev dependency
then search forbuild_runner
andhive_generator
.Mark the model as
HiveType
and importpart [model_name].g.dart
to generate the adapter in the next step.import 'package:hive_flutter/hive_flutter.dart'; part 'model.g.dart'; @HiveType(typeId: 0) class YourModel { @HiveField(0) final String property1; @HiveField(1) final String property2; YourModel(this.property1, this.property2); }
From the terminal, run
dart run build_runner build
to generate the HiveAdapter.Initialize HiveBox, register adapters(if any), and open the box with its name.
await Hive.initFlutter(); Hive.registerAdapter(YourModelAdapter()); await Hive.openBox('BoxName');
Get the box by its name and it’s ready to use.
final box = Hive.box('BoxName');