2

在我的应用程序中,我使用了 3 个相互嵌套的类,我决定使用似乎比 sqlite 更容易的 hive 包,我已经根据 hive.dev 设置了 hive 包,并且我没有遇到与设置过程相关的任何错误,我想将我的对象存储在蜂巢框中,当我创建主类别对象并添加到蜂巢框时,我可以在我的主页中收到它,但是当我想将子类别添加到任何特定的主对象时,我无法访问子类别列表,或者我不明白我到底要做什么,有人可以帮我创建必要的功能吗?我在下面分享了 3 个类和 hiveHelper 类。谢谢你的帮助。

part 'categoryModel.g.dart';

@HiveType(typeId : 0)
class CategoryModel extends HiveObject{
  CategoryModel(
      { this.categoryId,
        this.categoryImagePath,
        this.categoryName,
        this.categoryColor,
        this.subCategoryModels});

  @HiveField(0)
  final int categoryColor;

  @HiveField(1)
  List <SubCategoryModel> subCategoryModels=[];

  @HiveField(2)
  int categoryId;

  @HiveField(3)
  String categoryImagePath;

  @HiveField(4)
  String categoryName;


}
part 'subCategoryModel.g.dart';


@HiveType(typeId : 1)
class SubCategoryModel extends HiveObject{
  SubCategoryModel({
    this.subCategoryId,
    this.subCategoryImagePath,
    this.subCategoryName,
    this.categoryColor,
    this.recipeId,
    List<Ingredient>ingredients,
    this.recipePhotoDir,
    this.recordedVoiceDir,
    bool isCompeted});

  @HiveField(0)
  final int categoryColor;

  @HiveField(1)
  final double recipeId;

  @HiveField(2)
  bool isCompleted;

  @HiveField(3)
  int subCategoryId;

  @HiveField(4)
  String subCategoryImagePath;

  @HiveField(5)
  String subCategoryName;

  @HiveField(6)
  List <Ingredient> ingredients=[] ;

  @HiveField(7)
  String recipePhotoDir;

  @HiveField(8)
  String recordedVoiceDir;

}
@HiveType(typeId: 2)
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{

  void addCategoryModel(CategoryModel categoryModel)async{
    var box = await Hive.openBox('categoryModelsInBox');
    await box.add(categoryModel);
    await Hive.close();
  }
  Future <Map> getCategoryModels()async{
    var box = await Hive.openBox('categoryModelsInBox');
    var boxToMap=box.toMap();
    await Hive.close();
    return boxToMap;
  }

  void addSubCategory(SubCategoryModel subCategoryModel, key)async{
    var box = await Hive.openBox('categoryModelsInBox');
    box.put(key, subCategoryModel);
  }
}

4

0 回答 0