Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private Setters in Dart

Tags:

flutter

dart

I have been looking but couldn't find any reference to this. I would like to create a private setter in Dart to call an additional function after I have changed a private variable.

ViewState _state;

ViewState get state => _state;

set _state(value) {
  _state = value;
  notifyListeners();
}

How Can I achieve this?

like image 337
Filled Stacks Avatar asked Apr 19 '19 03:04

Filled Stacks


1 Answers

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.

like image 52
Scrobot Avatar answered Sep 28 '22 08:09

Scrobot