How to access to te nested objects element from the hive box?

Viewed 483

in my app I use 3 class which are nested with each other, I decided to use hive package which seems easier than sqlite, I have set up hive packages according to hive.dev and I dont face any error related to the setup process, I want to store my objects in the hive box, when I create main category object and I add to the hive box, I can recieve it in my home page, But when I want to add subcategory to any spesfic main object, I can not access the list of subcategories list, or I dont understand what I have to exactly, Can someone help me to create necessary function I share 3 classes and hiveHelper class below. Thanks for your help.

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);
  }
}

0 Answers
Related