Florian
Florian

Reputation: 25

The method 'addSystemCategories' was called on null. in Flutter

Why does this method call crashes in this error? I just want to add some data in my database when the switch is toggled.

The following NoSuchMethodError was thrown while handling a gesture: The method 'addSystemCategories' was called on null. Receiver: null Tried calling: addSystemCategories()

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

This is the method I call:

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);
  }

Upvotes: 0

Views: 20

Answers (1)

Andrej
Andrej

Reputation: 3225

In toggleCategoriesSupport you wrote: CategoriesProvider categoriesProvider;. You declared categoriesProvider but you didn't assign it a value, so it has the value null by default. You probably meant to say CategoriesProvider categoriesProvider = CategoriesProvider();.

Upvotes: 1

Related Questions