Search code examples
oopdartgetter-setter

Dart - set method not defined


I have two classes that work with each other, but some reason the set method in one of the classes is not considered defined in the other class. I am currently learning Dart (via Flutter), so I am wondering if I might be missing something.

class ClassA {
    List<ClassB> _bunchOfClassBs = [];

    void doSomething() {
        for(ClassB foo in _bunchOfClassBs) {
            foo.addCount('bar'); // Undefined method
        }
    }
}

class ClassB {
    int_counting = 0;

    set addCount(int number) => _counting += number;
}

Solution

  • You are calling setter incorrectly, should be:

    foo.addCount = 123;
    

    And for setters, name should not be addCount but count