What are the basics of Hive in Flutter?
In this shot, we will learn the basics of Hive in Flutter. The Hive manages data and stores it locally on a device in a Flutter application.
We can easily manage our database with
What is Hive?
According to Hive’s documentation:
“Hive is a lightweight and blazing fast key-value database written in pure Dart.”
Hive can save application data on a device and get it extremely fast whenever we need it.
Performance
Hive is much faster than most other popular local storage alternatives.
Usage
The usage of Hive in a Flutter project is different than using it in a pure Dart application. The only difference is in setup, which we’ll cover in this shot.
How to import the Hive package
First, we need to import the package. We’ll use the command below in the terminal:
flutter pub add hive
We can also manually import the Hive package to pubspec.yaml:
dependencies:hive: ^2.0.4
Note: The current version of Hive is
2.0.4. If you want the latest version, check it out on pub.dev.
To use Hive with Flutter, we also need to import the hive_flutter package with the following command:
flutter pub add hive_flutter
We can also manually import it to pubspec.yaml:
dependencies:hive_flutter: ^1.1.0
Setup
For Hive to work with Flutter, we need to set up some things first.
In the main.dart file, we need to change some things in the main method. When we create a new Flutter project, it looks as follows:
void main() {runApp(MyApp());}
The following code demonstrates how the Hive works in the main.dart file:
import 'package:hive/hive.dart';import 'package:hive_flutter/hive_flutter.dart';void main() async {// Allows for async code in main methodWidgetsFlutterBinding.ensureInitialized();// Initialises Hiveawait Hive.initFlutter();runApp(MyApp());}
The additional lines of code in the main method ensure that Hive is initialized before our application is run.
Boxes
Hive uses boxes as a place where the data is stored. We can think of the box as a container for data of a specific area of our application.
For example, our application needs to store user data and settings. In that case, two boxes named userBox and settingsBox should be enough.
How to open the box
To open a box, use the following command:
await Hive.openBox('boxName');
We’ll replace boxName with the name we want to give to our box.
The example with userBox and settingsBox is as follows:
await Hive.openBox('userBox');await Hive.openBox('settingsBox');
We need to open a box before we write to or read data from it. We’ll open a box just after we initialize Hive.
import 'package:hive/hive.dart';import 'package:hive_flutter/hive_flutter.dart';void main() async {WidgetsFlutterBinding.ensureInitialized();await Hive.initFlutter();// Opens a new box called 'myBox'await Hive.openBox('myBox');runApp(MyApp());}
How to access the box
To perform actions on the data in a box, we access it first, as shown below:
final box = Hive.box('boxName');
Let’s make sure that a box with that name has already been opened.
How to write data to a box
To write data to a box, we use the put method:
box.put('key', 'value');
Writing data to a box is similar to writing data to a map. We store values under specified keys.
Note: A value can be a number, list, or any other Dart type or object.
box.put('myString', 'Hello World!');box.put('myNumber', 7);box.put('myList', []);
How to read data from a box
To read data from a box, we use the get method:
final value = box.get('key');
If the key does not exist, the method returns null. To avoid this, we can specify an optional defaultValue parameter:
final value = box.get('nonexistingKey', , defaultValue: 'myDefaultValue');
How to delete key-value pairs
We can use the delete method to delete data that is not needed anymore. It accepts a specified key:
box.delete('myKey');
Conclusion
Managing our local database with Hive is super easy. If you want to learn more about Hive, please check out the official documentation.