'addSystemCategories' 方法在 null 上被调用。在颤振中

时间:2021-03-06 15:29:59

标签: flutter google-cloud-firestore

为什么这个方法调用会在这个错误中崩溃?我只想在切换开关时在我的数据库中添加一些数据。

<块引用>

处理手势时抛出以下 NoSuchMethodError: 'addSystemCategories' 方法在 null 上被调用。接收器:空 尝试调用:addSystemCategories()

void toggleCategoriesSupport () {
    CategoriesProvider categoriesProvider;
    categoriesProvider.addSystemCategories();
    _categoriesSupport = !_categoriesSupport;
    _saveToPreferences();
    notifyListeners();
  }

这是我调用的方法:

void addSystemCategories() {
    final categoryAll = ProductCategory(createdTime: DateTime.now(), id: '0', title: 'Alle', type: 'category');
    final categoryNothing = ProductCategory(createdTime: DateTime.now(), id: '1', title: 'Keine', type: 'category');
    addCategory(categoryAll);
    addCategory(categoryNothing);
  }

1 个答案:

答案 0 :(得分:1)

您在 toggleCategoriesSupport 中写道:CategoriesProvider categoriesProvider;。您声明了 categoriesProvider 但没有为其分配值,因此默认情况下它的值为 null。您可能想说CategoriesProvider categoriesProvider = CategoriesProvider();

相关问题