Private Setters in Dart

There is nothing wrong with the private setter, just the choices of naming.

Just as you couldnt use state as the name of the variable because it was the name of the getter, you cant use _state for the variable and setter.

It is kind of ugly but using __state for your variable (and updating the getter and setter accordingly) should get the job done.

Edit: As mentioned in the comments, the name __state is not important, it just needs to be a name that is not taken by the getter/setter. Variable _myState with getter state and setter _state works just as well.


Dart is no allowing private setters, it's true. You can hack it with your own private function

ViewState _state;

ViewState get state => _state;

void _changeState(value) {
  _state = value;
  notifyListeners();
}

here my sample on DartPad where you can make some experiments.

Tags:

Dart

Flutter