您好,我有三个嵌套类,我希望将它们存储在 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.categoryColor,
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;
}
}