未处理的异常:NoSuchMethodError:在 null 上调用了方法“add”:尝试调用:add(Instance of 'CategoryModel')

问题描述

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

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

解决方法

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

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

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