Flutter - Create TypeAdapters in Hive
Last Updated :
28 Apr, 2025
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.
Similar Reads
Flutter - TypeWriter Text Effect In this Flutter article, let's learn how to create Animated TypeWritting Effect on a flutter Text Widget. The typewriter effect is a kind of animation where the text appears to be typed out letter by letter. This is often used in games, movies, videos, or in certain apps where you want the userâs at
7 min read
Flutter - Create Option Menu for ListView ListView is the efficient widget to display a list of items. Sometimes we need some options on individual items in the ListView. To create an options menu for a ListView in Flutter, you can use the PopupMenuButton widget along with PopupMenuItem or PopupMenuItemBuilder. This allows you to display a
5 min read
Flutter - Store Data in Hive Local Database Hive is a data storage system on our phone where we can store data in boxes. We can store an integer, string, list of strings, Boolean, double, models, integers, etc., in Hive. Now, let us discuss where we can implement these. The first way we can use this is to save the user information user is log
12 min read
Insert Operation in DataTable in Flutter A DataTable is a material design used to display data on a table or in rows and columns. Â A Data table is used to show the data which have columns and rows as child, a Column is used to set the name of the column, and a Row is used to set the values of the columns. Any column in a DataTable can be s
5 min read
Flutter - Integrate Google Gemini API Currently, Artificial Intelligence is blooming all around. You must have heard about the ChatGPT, Bard and now Google has come up with a very new Artificial intelligence Gemini which responds to users in a truly human-like way. You can connect Google Gemini with Flutter in a very simple and quick wa
6 min read