如何在 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 (将#修改为@)