How to use Customer TypeAdapter (Hive/Flutter)?
Yes, You can always do it.
First get these plugins, hive, hive_flutter
in the dependencies in the pubspec.yaml.
and in the pubspec file there is also a section of dev dependencies
there add, hive_generator: ^1.1.0 build_runner: ^2.0.1
And you are good to start.... First Write your Class in this Format,
import 'package:hive/hive.dart'; //import Hive
part 'weather_model.g.dart';
// Specify the Location of the type-adapter's Location (specifically)
//Add the .g.dart extension
// at last of the same File name you are using
// and add "part" keyword before all.
@HiveType(typeId: 0) //Add this Line
class WeatherModel {
@HiveField(0) //Add this Line, From Index 0... and so on.
String user_city; // Your Class Materials...
@HiveField(1) //Add this Line, From Index 0... and so on.
bool celciusMetric;
WeatherModel(
{this.user_city,
this.celciusMetric}); //Constructor here..
// flutter packages pub run build_runner build
}
After you read the whole commented code above,
And Implemented according to your use.
Go to your Terminal and write there, flutter packages pub run build_runner build --delete-conflicting-outputs
and you are good to go, you will find
Now go to Your main.dart
and import all the stuffs of hive.
GitGud31
Updated on December 28, 2022Comments
-
GitGud31 over 1 year
I'm not quite sure on how to use Hive DB in Flutter. I mean I have a ```WooCustomer```` model class and I want to store it locally (once the customer logs in).
My question is, do I have to convert that
WooCustomer
toHiveObject
then create theTypeAdapter
or do I createTypeAdapter<WooCustomer>
directly?PS: The WooCustomer is an external pkg.
Is this the correct way to implement
TypeAdapter<WooCustomer>
?class DatabaseAdapterService extends TypeAdapter<WooCustomer> { @override final int typeId = 0; @override WooCustomer read(BinaryReader reader) { return WooCustomer() ..id = reader.read() ..username = reader.read() ..firstName = reader.read() ..lastName = reader.read() ..email = reader.read() ..password = reader.read() ..avatarUrl = reader.read() ..role = reader.read() ..dateCreated = reader.read() ..dateCreatedGmt = reader.read() ..dateModified = reader.read() ..dateModifiedGmt = reader.read() ..isPayingCustomer = reader.read() ..links = reader.read() ..metaData = reader.read() ..billing = reader.read() ..shipping = reader.read(); } @override void write(BinaryWriter writer, WooCustomer customer) { writer.write(customer.username); writer.write(customer.id); writer.write(customer.firstName); writer.write(customer.lastName); writer.write(customer.email); writer.write(customer.password); writer.write(customer.links); writer.write(customer.avatarUrl); writer.write(customer.role); writer.write(customer.metaData); writer.write(customer.dateCreated); writer.write(customer.dateCreatedGmt); writer.write(customer.dateModified); writer.write(customer.dateModified); writer.write(customer.dateModifiedGmt); writer.write(customer.isPayingCustomer); writer.write(customer.billing); writer.write(customer.shipping); } }