Git*_*d31 5 dart woocommerce flutter flutter-hive
我不太清楚如何在 Flutter 中使用 Hive DB。我的意思是我有一个“WooCustomer”模型类,我想将其存储在本地(一旦客户登录)。
我的问题是,我是否必须将其转换WooCustomer
然后HiveObject
创建TypeAdapter
,还是TypeAdapter<WooCustomer>
直接创建?
PS:WooCustomer 是一个外部 pkg。
这是正确的实施方法吗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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
是的,你总是可以做到。首先获取这些插件,hive, hive_flutter
位于 pubspec.yaml 的依赖项中。在 pubspec 文件中还有一段dev dependencies
添加,hive_generator: ^1.1.0 build_runner: ^2.0.1
你很高兴开始......首先以这种格式编写你的课程,
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
}
Run Code Online (Sandbox Code Playgroud)
当您阅读上面的整个注释代码后,并根据您的使用情况进行实现。转到您的终端并在那里写入,flutter packages pub run build_runner build --delete-conflicting-outputs
然后您就可以开始了,您会发现
现在转到 Yourmain.dart
并导入 hive 的所有内容。