flutter - 如何使用嵌套类互相与蜂巢?
问题描述
您好,我有三个嵌套类,我希望将它们存储在 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;
}
}
解决方案
推荐阅读
- tcsh - 在 GNU 并行中排除某些模式
- java - 如何使用 selenium java 输入文本
- mysql - VB:如何通过 StringBuilder 将查询结果插入到 .xlsx 文件中
- java - 我一直在 android studio 中制作一个 News API 项目,但是 volley 有错误。错误 - com.android.volley.AuthFailureError。使用 json
- excel - 清除内容后Excel计算缓慢
- python - 一篇文章的不同 url 路径(Django)
- python - 遍历 Pandas 中具有唯一 col 值的行组
- javascript - 为什么我的状态变量未为 props.location.state 定义?
- php - /usr/libexec/postfix/smtp 在 macOS 上不使用 Laravel 发送邮件
- mysql - 如何根据满足SQL条件的最后一条记录创建视图