如何从配置单元访问嵌套对象元素?

问题描述

在我的应用程序中,我使用了 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.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);
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)