【问题标题】:how to delete all from Moor?如何从 Moor 中删除所有内容?
【发布时间】:2019-10-09 10:21:49
【问题描述】:

我尝试在退出时全部删除。

onPressed: () {
       _categoryBloc.deleteEntry();
 }

但是错误来了

未处理的异常:NoSuchMethodError:方法“deleteEntry”是 调用为空。 E/颤动​​(9255):接收器:空 E/颤动(9255): 尝试调用:deleteEntry()

初始集团

 CategoryBloc _categoryBloc;

MyBloc 类

class CategoryBloc {
...
      void deleteEntry() {
        _categoryRepository.deleteCategory();
      }
...
}

我的回购类

deleteCategory() async {
    try{
      await _categoryDao.deleteEntry();
    }catch (e){
      print('Caught in delete ${e.body}');
      rethrow;
    }
  }

道类

  Future deleteEntry() {
    return delete(categories).delete(Categorie());
  }

数据类

class Categorie extends DataClass implements Insertable<Categorie> {
  final String id;
  final bool isActive;
  final String categoryName;
  final int displayOrder;
  Categorie(
      {@required this.id,
      @required this.isActive,
      @required this.categoryName,
      @required this.displayOrder});
  factory Categorie.fromData(Map<String, dynamic> data, GeneratedDatabase db,
.....

【问题讨论】:

  • 你们都分配_categoryBloc_categoryDao吗?该异常告诉您您正在尝试对空值调用deleteEntry。这可能在您的小部件中或您的 repo 类中(不确定是哪个,因为您没有包含堆栈跟踪)
  • 是的,你可能只是忘了实例化你的 bloc
  • 为什么问题投反对票?
  • @HannesKüttner 我添加了我自己的答案。谢谢支持
  • @RodrigoBastos 我添加了我自己的答案。谢谢支持

标签: flutter dart


【解决方案1】:

像这样改变onPressed方法

ProxyProvider<CategoriesRepository,CategoryBloc>(
          builder: (context, categoryRepo, categoryBloc)=>
              CategoryBloc(categoryRepository: categoryRepo),
          dispose: (context, categoryBloc)=> categoryBloc.dispose(),
          ).didChangeDependencies(context, _categoryBloc).deleteEntry();

如果你想删除所有你需要检查的查询,如下所示:

  deleteEntry() {
    try{
      return delete(subCategories).go();//like this
    }catch (e){
      print("error is $e");
    }
  }

【讨论】:

    最近更新 更多