I want to pass the setter of a class' field as parameter to a function so the function can do the assignment.
Is there a way without using reflection?
The setter method takes a parameter and assigns it to the attribute. Getters and setters allow control over the values.
Setters only take one parameter because they are special methods that expect a value and set the property to that value, upon validation of type.
You can't directly pass the setter.
To avoid reflection you can wrap the setter inside a function :
class A {
String _attr=;
set attr(String v) => _attr = v;
}
main() {
final a = new A();
// create a wrapper function to set attr
final setter = (v) => a.attr = v;
callSetter(setter);
print(a._attr);
}
callSetter(setterFunction(value)) {
setterFunction("value");
}
This proposal about generalized tear offs is approved and will probably implemented soon and allows to closurize getters and setters like:
var setter = a#attr;
// and can be invoked like
setter(value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With