如何解决如何在 hive 中使用嵌套类彼此?
您好,我有三个嵌套类,我希望将它们存储在 hive 包提供的框中,我插入了一些代码,但我不知道这是正确的方法,我该如何存储它们,有人可以帮助我吗?
@HiveType(typeId : 1)
class CategoryModel extends HiveObject{
CategoryModel(
{ this.categoryId,this.categoryImagePath,this.categoryName,this.categoryColor,this.subCategoryModels});
@HiveField(0)
final int categoryColor;
@HiveField(1)
HiveList <SubCategoryModel> subCategoryModels;
@HiveField(2)
int categoryId;
@HiveField(3)
String categoryImagePath;
@HiveField(4)
String categoryName;
}
@HiveType(typeId : 2)
class SubCategoryModel extends HiveObject{
SubCategoryModel({
this.subCategoryId,this.subCategoryImagePath,this.subCategoryName,this.recipeId,HiveList<Ingredient>ingredients,this.recipePhotoDir,this.recordedVoiceDir,bool isCompeted});
@HiveField(0)
final Color categoryColor;
@HiveField(1)
final double recipeId;
@HiveField(2)
bool isCompleted;
@HiveField(3)
int subCategoryId;
@HiveField(4)
String subCategoryImagePath;
@HiveField(5)
String subCategoryName;
@HiveField(6)
HiveList <Ingredient> ingredients ;
@HiveField(7)
String recipePhotoDir;
@HiveField(8)
String recordedVoiceDir;
}
@HiveType(typeId: 3)
class Ingredient extends HiveObject{
Ingredient({this.ingredientName,this.dropDownValue,this.ingredientAmount});
@HiveField(0)
final ingredientName;
@HiveField(1)
String dropDownValue;
@HiveField(2)
String ingredientAmount;
}
class HiveHelper{
Box <CategoryModel>categoryModels;
CategoryModel categoryModel;
void initState() async {
if(categoryModels==null){
var _directory =await getApplicationDocumentsDirectory();
Hive
..init(_directory.path)
..registeradapter(CategoryModelAdapter())
..registeradapter(SubCategoryModelAdapter())
..registeradapter(IngredientAdapter());
categoryModels = await Hive.openBox<CategoryModel>('categoryModels');
}
}
void addCategoryModel(CategoryModel categoryModel)async{
await categoryModels.add(categoryModel);
}
Future <Box> getCategoryModels()async{
return categoryModels;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。