如何从Moor中删除所有内容?

时间:2019-10-09 10:21:49

标签: flutter dart

我尝试在退出时删除所有内容。

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

但是错误来了

  

未处理的异常:NoSuchMethodError:方法“ deleteEntry”为   调用为null。 E / flutter(9255):接收器:空E / flutter(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;
    }
  }

dao类

  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,
.....

1 个答案:

答案 0 :(得分:0)

更改了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");
    }
  }