2. What is Hive?
Hive is the lightweight, NoSql, key-value database that is used to store the data
locally in flutter and dart applications.
Hive is very helpful and is very simple to utilize.
It consists of the key-value database without numerous relations.
Hive is the offline database.
3. Box
Hive stores its data in boxes containing key-value sets.
For a small app, a single box might be enough.
For more advanced problems, boxes are a great way to organize your data. Boxes
can also be encrypted to store sensitive data.
Before using the box, the hive box needs to be opened first.
4. Open Box
var box = await Hive.openBox<DataModel>('hive_box');
Or
var box = await Hive.openBox('hive_box');
Either way, you can open the box.
If the box is already opened, it is returned and all supplied parameters are ignored.
Without opening the hive box, you cannot read, write, and delete the data.
5. Get opened box
var box = Hive.box('myBox');
This procedure or method is mostly useful for Flutter apps as you
do not have to pass the box between the widgets.
6. Close box
await box.close();
In case, if you do not need the box again, you should close it. All the cached keys and values of
the box will be dropped from the memory and the box file is closed after all read and write
operations are completed.
You can leave the box open if you need a box again in the future. It is ok to leave a box open until
the app is running.
7. Write
var box = Hive.box('box_name');
box.put('name', 'John Doe');
box.put('friends', ['Hari', 'Ram', 'Sita']);
box.put(123, 'testdata');
box.putAll({'key1': 'value1', 23: 'process'});
Writing to a box is very easy and is almost like writing to a map.
8. Read
var box = Hive.box('myBox');
String name = box.get('name');
DateTime birthday = box.get('birthday');
9. Read
If the key is not present, null is returned. Optionally you can also a defaultValue
that can be returned in the case the key does not exist.
double length=box.get('yourkey',defaultValue:12.5);
10. Type Adapters
Hive provides us permission to almost all primitive data types such as String, int,
Map, List, DateTime and Uint8List.
Type Adapters convert the object from and to binary form.
You need to generate the TypeAdapter or you can write on your own.
You need to register the adapter before using it.
12. Why Hive Database?
● It is the most efficient database in terms of speed and performance compared
to other databases like SQLite and SharedPreferences.
● It gives a straightforward method to perform CRUD operations.
● Built-in strong encryption
● No native dependencies
13. When to use Hive
Hive can be used to store almost every sort of data.
● Cart contents
● Blogs/Articles
● Messages
● Categories of products
● Session
● etc
14. When not to use Hive
Every type of data can be stored in Hive when it is modeled in a correct manner.
Sometimes it would be more convenient to use a relational database like SQLite
as it is more convenient but because it is not faster.
You should consider using SQLite if your data contains complex relations and you
have to rely mostly on indices and complex queries.
As Hive is so lightweight, you can use Hive and other databases together.