title: How to simulate variable update by reference in Dart?
body: <p>Currently I have LOTS of this in a class (a <code>ChangeNotifier</code> so pretty much one for each field):</p>
  
  <pre class="lang-dart prettyprint-override"><code>String _name;
  String get name =&gt; _name;
  set name(String value) {
    if (_name == value) {
      return;
    }
    _name = value;
    notifyListeners();
  }
  </code></pre>
  
  <p>In C# I had the same problem with <code>INotifyPropertyChanged</code>, but there we can pass variables by reference, so it's <a href="https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist">easier to make a function for encapsulating this kind of behavior</a>, and many libraries actually supply them.</p>
  
  <p>I came up with this, but I am not sure if there are better (simpler) alternatives:</p>
  
  <pre class="lang-dart prettyprint-override"><code>typedef ValueSetter&lt;T&gt; = void Function(T); // or imported from [ChangeNotifier]
  
  ...
  
  String _name;
  String get name =&gt; _name;
  set name(String value) =&gt; _setAndNotifyIfNotEqual(_name, value, (v) =&gt; _name = v);
  
  /// If [newValue] is different than [oldValue], call [setter] with [newValue],
  /// then call [notifyListeners].
  void _setAndNotifyIfNotEqual&lt;T&gt;(T oldValue, T newValue, ValueSetter setter) {
    if (newValue != oldValue) {
      setter(newValue);
      notifyListeners();
    }
  }
  </code></pre>
  
  <p>Are there any better ways of achieving this in Dart?</p>