I made a class and not sure why my getter is fine but the setter is showing as not defined in flutter.

class Test {
  int range = 1000;

  set setRng(int val) => range = val;

  get getRng => range;
}

Test.getRng no errors..

Test.setRng(100) throws the error The method 'setRng' isn't defined for the class 'Test'

Clearly they're both defined..?


Solution 1: CoderCharmander

In Dart, you invoke setters with setter = value;. So your code can be modified like this:

test.setRng = 0;

Getters and setters make member functions look like member variables. By convention, they can then have variable like names, for example:

class Test {
  int _range = 1000;

  set range(int val) => _range = val; // optionally perform validation, etc

  int get range => _range;
}

now it looks more natural when you use:

test.range = 123; // using the setter
print(test.range); // using the getter