Flutter – Create TypeAdapters in Hive
Flutter is open source framework from which we can create cross-platform applications with a single codebase. Hive is a local device storage that stores data in your phone storage. We store user profiles and tokens whether they are logged in or not like that thing. To deeply understand what hive please refer to this article Flutter – Store Data in Hive Local Database. In this article, we have covered almost all the data types but just missed 1 important data type i.e. TypeAdaptors we can understand this simply like models.
Step By Step Implementation
Let’s learn how we can create a type adapter ( it will be automatically created) in Hive. We need to show how our model class will look like
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the packages
We will add the following package in folder
Dart
dev_dependencies: hive_generator: ^1.1.2 build_runner: ^2.1.8 |
Step 3: Create a sample model file
We will create a sample model file
Sample Json data:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
Create a model file for this sample data:
Dart
import 'package:hive/hive.dart' ; part 'post_models.g.dart' ; @HiveType(typeId: 1) class PostModel extends HiveObject { @HiveField(0) // Hivefield id in brackets final int id; @HiveField(1) final int userId; @HiveField(2) final String title; @HiveField(3) final String body; PostModel({ required this .body, required this .id, required this .userId, required this .title, }); } |
Important: During this creation you might get error on 3rd line but it will get remove after doing next steps
Step 4: Run the command to create a hive model class
Now we will run the command it will automatically create a model file named as post_models.g.dart its name is same as model class it will just create new file with .g in it.
Dart
flutter packages pub run build_runner build |
To delete conflict during type adapters creation
Dart
flutter packages pub run build_runner build -- delete -conflicting-outputs |
Step 5: Now we will register the type adapters in main function
We will register the type adapters like below
Dart
Future main() async { // It is used so that void main function can // be intiated after successfully intialization of data WidgetsFlutterBinding.ensureInitialized(); // To intialise the hive database await Hive.initFlutter(); Hive.registerAdapter(PostModelAdapter()); // Open Box for Post model await Hive.openBox<PostModelAdapter>( "postBox" ); runApp( const MyApp()); } |
Output:
Folder before type adaptors creation:
Folder after type adaptors creation:
Now you can access the box just like other box. In addition to that, do you know hive provide a lazy box also that means
Dart
var lazyBox = await Hive.openLazyBox( 'myLazyBox' ); |
Let’s understand what lazybox is? A lazy box is meant to reduce the amount of memory used by saving only the keys and not the contents of the box when it is opened. In this manner, Hive may obtain a value using the previously saved keys whenever a user requires it.
Contact Us