![]() |
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 ImplementationLet’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 StudioTo 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 packagesWe will add the following package in folder Dart
Step 3: Create a sample model fileWe will create a sample model file Sample Json data:{ Create a model file for this sample data:Dart
Step 4: Run the command to create a hive model classNow 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
To delete conflict during type adapters creation Dart
Step 5: Now we will register the type adapters in main functionWe will register the type adapters like below Dart
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
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. |
Reffered: https://www.geeksforgeeks.org
Dart |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |