Hive for flutter with example
Hive is lightweight key-value database written in dart. It is an alternative to sqlite and with nosql, and which is having feature like data persistency.
To use the hive add the below dependencies:
Hive:
Hive_flutter:
Path_provider:
In hive, database is nothing not but a box, box is just like a table .
1. How to create the Box?
Box box;
@override
Void initState( ){
Super.initState( );
openBox( );
}
You just open the box in the initState method like below code. For opening the box we use future method.
Future openBox( ) async {
Var dir = await getApplicationDocumentDirectory( );
Hive.init(dir.path);
box = await Hive.openBox(‘testBox’);
Return;
}
Here, the dir variable contains the application directory and next we create the database in this location. Hive.openBox method creates the box/database for your app with given name.
Now database is ready for operations.
Data Inserting or Put Data:
box.put(‘name’,”satheesh”);
simply we can insert as key value pair combination to the box.
Data Extracting or Get Data:
box.get(‘name’);
Here simply provides the key and it returns the value.
Update Data:
box.put(‘name’, “Mahesh”);
Here the name key is already there, so when when use put with the same name the value gets updated. If key is not there it creates a new key value pair.
Delete Data :
box .delete(‘name’);
This method will deletes the value by using the given key.
Get All values from Box:
box.toMap( ) ;
Will returns all the data which box have.
How can we Insert without key?
Sometimes user wants to enter some data which does not have the keys then we can add the data like below.
box.add(‘satheesh’);
This method takes key as 0 index, when you add one more name it takes the key as 1 index.
How to create box for user defined class/ model class:
Lets crate a model class
Class TaskModel {
Final String title;
Final String detail;
Final bool incompleted;
TaskModel({ this.title, this.detail,this.incompleted} );
}
But Hive supports only primitive data to insert, but how can we add the model class object to hive. For that we need to create the adapter for model class.
How to create the Adapter for model class?
We need the below dependencies to our project
Hive_generator:
Build_runner:
To get the adapter of model class we have to add some to extra information to model class, that hive can use it later.
For class name:
@HiveType( typeId : 0)
Class TaskModel {
}
This annotation helps to create the builder and for class we are giving type id any number.
In the same way we can add hive annotation to fields also
@HiveType( typeId : 0)
Class TaskModel {
@HiveField(0)
Final String title;
@HiveField(1)
Final String detail;
@HiveField(2)
Final bool incompleted;
}
Add finally we have generate the adapter and add one more line to the model class as
Part ‘task_model.g.dart’;
Everything is done, now we have to run the build runner command in terminal.
Like below:
>flutter packages pub run build_runner build
It generates the adapter file and if you get any errors please check the dart version in pubspec.yaml file.
The adapter file name would be like this TaskModelAdapter the extra Adapter string added to your model class.
Everything is done, just add the adapter to Hive like below.
Future openBox( ) async {
Var dir = await getApplicationDocumentDirectory( );
Hive.init(dir.path);
box = await Hive.openBox(‘testBox’);
Return;
}
This code we used to crate the testBox now we have to add adapter to insert the model class object.
Future openBox( ) async {
Var dir = await getApplicationDocumentDirectory( );
Hive.init(dir.path);
Hive.registerAdapter(TaskModelAdapter( ) );
box = await Hive.openBox<TaskModel>(‘taskModelBox’);
Return;
}
That’s it now you can perform crud operations as we did earlier in this article.
Data Inserting :
TaskModel obj = TaskModel (
String title=” android”
Final String detail = “it is open source”;
Final bool incompleted = false;
);
box.add(obj);
I hope you like my article and all the best.
If you found this article helpful click and hold 👏 a button and show some love and help others to find this article.
Feeling more generous, you can buy me a cup of tea.