-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
I'm trying to make a macro that generates getters and setters that the user can easily initialize in the constructor.
The only way to do that, as far as I am aware, is to do something like:
class GeneratedParentClass {
int? _x;
int? get x => _x;
set x(int? val) {
_x = val;
//Do something interesting
}
GeneratedParentClass(int? x) : _x = x {
this.x = x;
}
}
class BaseClass extends GeneratedParentClass {
BaseClass(super.x);
}
in the build_runner system, this works pretty well, but in Macros, this appears to be impossible.
unlike appendMixins or appendInterefaces, extendsType requires a NamedTypeAnnotationCode, which requires an identifier.
I tried to make a FakeIdentifier which extended Identifier, and took a string, but unfortunately that crashed the inspector.
I think the ability to use created classes for extension would make macros significantly more robust and intuitive, since it would allow for a couple things:
- overrriding methods created by the macro.
For example, if a user wants to modify the result of a generated toJson() call, they could simply do:
@override
toJson() {
var json = super.toJson();
//modify json as desired
}
-
Guarantee the ability for the macro to initialize code as needed.
-
allow generated variables to be set in constructors. (Which is very important for most widget based code)