如何在 Flutter 中使用 Hive 进行库存列表?
How to stock list with Hive in Flutter?
我希望将一些列表存储到 Hive 中。
在我的 main.dart 中,我得到:
Hive.registerAdapter(MedicalConstantsAdapter());
await Hive.openBox(MEDICAL_CONSTANTS);
在我的 medical.dart 中,我得到:
@HiveType(typeId: 0)
class MedicalConstants extends HiveObject {
@HiveField(0)
List<SystolicPressure> systolicPressure;
@HiveField(1)
List<DiastolicPressure> diastolicPressure;
MedicalConstants({
this.systolicPressure,
this.diastolicPressure
});
MedicalConstants.fromJson(Map<String, dynamic> json) {
if (json['systolic_pressure'] != null) {
systolicPressure = new List<SystolicPressure>();
json['systolic_pressure'].forEach((v) {
systolicPressure.add(new SystolicPressure.fromJson(v));
});
}
if (json['diastolic_pressure'] != null) {
diastolicPressure = new List<DiastolicPressure>();
json['diastolic_pressure'].forEach((v) {
diastolicPressure.add(new DiastolicPressure.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.systolicPressure != null) {
data['systolic_pressure'] =
this.systolicPressure.map((v) => v.toJson()).toList();
}
if (this.diastolicPressure != null) {
data['diastolic_pressure'] =
this.diastolicPressure.map((v) => v.toJson()).toList();
}
return data;
}
}
class MedicalConstantsAdapter extends TypeAdapter<MedicalConstants> {
@override
final typeId = 1;
@override
MedicalConstants read(BinaryReader reader) {
return MedicalConstants()
..diastolicPressure
..systolicPressure = reader.read();
}
@override
void write(BinaryWriter writer, MedicalConstants obj) {
writer
..write(obj.systolicPressure)
..write(obj.diastolicPressure);
}
}
最后,在我的 session.dart 中,我得到了:
Hive.box(MEDICAL_CONSTANTS).values.toList().forEach((element) {
print(element);
});
我得到的 2 个列表来自我的端点和列表中的数据库存。
我只是暂时把我的结果打印出来。
我尝试使用配置单元 flutter packages pub run build_runner build
中的命令,但没有任何反应,这就是为什么我的 MedicalConstantsAdapter 未正确构建的原因?
有人知道如何正确使用它吗?
medical.dart
// add 'g' to file name to indicate generated
part 'medical.g.dart'
@HiveType(typeId: 0)
class MedicalConstants extends HiveObject {
@HiveField(0)
List<SystolicPressure> systolicPressure;
@HiveField(1)
List<DiastolicPressure> diastolicPressure;
MedicalConstants({
this.systolicPressure,
this.diastolicPressure
});
}
一旦你运行
,类型适配器就会生成到medical.g.dart
flutter packages pub run build_runner build
我希望将一些列表存储到 Hive 中。
在我的 main.dart 中,我得到:
Hive.registerAdapter(MedicalConstantsAdapter());
await Hive.openBox(MEDICAL_CONSTANTS);
在我的 medical.dart 中,我得到:
@HiveType(typeId: 0)
class MedicalConstants extends HiveObject {
@HiveField(0)
List<SystolicPressure> systolicPressure;
@HiveField(1)
List<DiastolicPressure> diastolicPressure;
MedicalConstants({
this.systolicPressure,
this.diastolicPressure
});
MedicalConstants.fromJson(Map<String, dynamic> json) {
if (json['systolic_pressure'] != null) {
systolicPressure = new List<SystolicPressure>();
json['systolic_pressure'].forEach((v) {
systolicPressure.add(new SystolicPressure.fromJson(v));
});
}
if (json['diastolic_pressure'] != null) {
diastolicPressure = new List<DiastolicPressure>();
json['diastolic_pressure'].forEach((v) {
diastolicPressure.add(new DiastolicPressure.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.systolicPressure != null) {
data['systolic_pressure'] =
this.systolicPressure.map((v) => v.toJson()).toList();
}
if (this.diastolicPressure != null) {
data['diastolic_pressure'] =
this.diastolicPressure.map((v) => v.toJson()).toList();
}
return data;
}
}
class MedicalConstantsAdapter extends TypeAdapter<MedicalConstants> {
@override
final typeId = 1;
@override
MedicalConstants read(BinaryReader reader) {
return MedicalConstants()
..diastolicPressure
..systolicPressure = reader.read();
}
@override
void write(BinaryWriter writer, MedicalConstants obj) {
writer
..write(obj.systolicPressure)
..write(obj.diastolicPressure);
}
}
最后,在我的 session.dart 中,我得到了:
Hive.box(MEDICAL_CONSTANTS).values.toList().forEach((element) {
print(element);
});
我得到的 2 个列表来自我的端点和列表中的数据库存。
我只是暂时把我的结果打印出来。
我尝试使用配置单元 flutter packages pub run build_runner build
中的命令,但没有任何反应,这就是为什么我的 MedicalConstantsAdapter 未正确构建的原因?
有人知道如何正确使用它吗?
medical.dart
// add 'g' to file name to indicate generated
part 'medical.g.dart'
@HiveType(typeId: 0)
class MedicalConstants extends HiveObject {
@HiveField(0)
List<SystolicPressure> systolicPressure;
@HiveField(1)
List<DiastolicPressure> diastolicPressure;
MedicalConstants({
this.systolicPressure,
this.diastolicPressure
});
}
一旦你运行
,类型适配器就会生成到medical.g.dart
flutter packages pub run build_runner build