gpt4 book ai didi

flutter - 如何在Flutter中使用Hive进行库存 list ?

转载 作者:行者123 更新时间:2023-12-03 03:33:53 29 4
gpt4 key购买 nike

我正在寻找一些Hive list 。

在我的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不能正确构建?

是否有人知道如何正确使用它?

最佳答案

医疗 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

关于flutter - 如何在Flutter中使用Hive进行库存 list ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62195285/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com