Managing data and storing it locally on a device in a Flutter app can be quite challenging. Fortunately, with
According to Hive’s documentation:
“Hive is a lightweight and blazing fast key-value database written in pure Dart.”
With Hive, we can save application data on a device and get it extremely fast whenever we need it.
Hive is much faster than most other popular local storage alternatives.
Using Hive in a Flutter project is a bit different than using it in a pure Dart application. The only difference is in setup, which we’ll cover in this shot.
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
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()); }
For Hive to work, it must look something like the following code:
import 'package:hive/hive.dart'; import 'package:hive_flutter/hive_flutter.dart'; void main() async { // Allows for async code in main method WidgetsFlutterBinding.ensureInitialized(); // Initialises Hive await Hive.initFlutter(); runApp(MyApp()); }
The additional lines of code in the main
method make sure that Hive is initialized before our application is run.
Hive uses boxes as a place where the data is stored. Let’s 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 more than enough.
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 user and settings will look 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()); }
To perform actions on the data in a box, we need to 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.
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 that 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', []);
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');
Sometimes we want to delete data that we don’t need anymore. We can use the delete
method, which accepts a specified key:
box.delete('myKey');
Managing our local database with Hive is super easy. If you want to learn more about Hive, please check out the official documentation.
RELATED TAGS
CONTRIBUTOR
View all Courses