0

在我的应用程序中,我使用 Hive 包将数据保存到数据库中,在主页中,当我创建一个名为 CategoryModel 的对象时,当我尝试将其添加到框中时,我遇到了标题错误。我在下面分享 homePage initState 和 HiveHelper 类。

HiveHelper hiveHelper =HiveHelper();
  CategoryModel newCategoryModel=CategoryModel();
  Widget buildBottomSheet(BuildContext context)=>AddMenuScreen(buttonText: "Menü Ekle",route: "homeScreen");
  int flag=0;
  List list=[];

 void initState(){
    super.initState();
    hiveHelper.initState();
    if (widget.newCategoryName!=null && widget.newCategoryImagePath!=null){
      newCategoryModel= CategoryModel(categoryId:categoryModels.length-1, categoryImagePath: widget.newCategoryImagePath,
        categoryName: widget.newCategoryName,categoryColor: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.5).value);
      categoryModels.insert(categoryModels.length-1,newCategoryModel);
      hiveHelper.addCategoryModel(newCategoryModel);
    }

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(SubCategoryModelAdapter());

      categoryModels = await Hive.openBox<CategoryModel>('categoryModels');

    }
  }
  void addCategoryModel(CategoryModel categoryModel)async{
    await categoryModels.add(categoryModel);
  }
  Future <Box> getCategoryModels()async{
    return categoryModels;
  }
}

我该如何处理这个问题请帮助我..

4

0 回答 0