Search code examples
dartsetterdart-mirrors

In Dart what is the generated name for a setter method?


If I have a class with a setter defined, how do I reference then generated method as a function from an instance of that class. The spec sort of suggests it would be the id of the variable + '=" (seems daft), but this doesn't parse.

So for example:

class Bar {

  set foo(int value) {
  //whatever
  }
}

typedef F(int value);

void main() {
  F f = new Bar().foo=; //Fails, but what should this be??
  f(5);
}

Solution

  • The setter is named foo= but this is not something you can reference in the way you want. Even looking at dart:mirrors the MethodMirror (the mirror for object methods including setters) has no way of invoking it. You could easily rewrite this as:

    class Bar {
    
      set foo(int value) {
      //whatever
      }
    }
    
    typedef F(int value);
    
    void main() {
      Bar b = new Bar();
      F f = (int value) => b.foo = value;
      f(5);
    }