Link to Getter / Setter Functions

I have the following variable and getter / setter defined in my data model:

class Actor { int _x; int get x => _x; set x(int value) => _x = value; } 

And there is this generic class that requires a getter / setter function pointer

 class PropertyItem { var getterFunction; var setterFunction; PropertyItem(this.getterFunction, this.setterFunction); } 

How to pass getter / setter X function reference to PropertyItem class?

 // Something like this var item = new PropertyItem(x.getter, x.setter); 

EDIT: Updated with a clearer question

+4
source share
2 answers

In short, you are not doing this. Getters and setters are not retrieved - they are indistinguishable from the presence of a field (unless you make side effects, of course).

In your example, you can simply do:

 class Actor { int x; } 

and get exactly the same effect.

What you want for some actor "actor" to independently perform the functions:

 var item = new PropertyItem(() => actor.x, (v) { actor.x = v; }); 

This proposal for generalized passages is approved and is likely to be implemented in the near future and will allow to fix getters and setters, for example:

 var item = new PropertyItem(actor#x, actor#x=); 
+6
source

In Dart, the following:

 class Foo { int _offsetX; int get offsetX => _offsetX; set offsetX(int ox) => _offsetX = ox; } 

is equivalent to:

 class Foo { int offsetX; } 
+3
source

All Articles