I created a library in Flutter to generate code for the annotated elements. I need to check the parameter in the constructor of class is required using analyzer.
Example of widget with annotation:
@myAnnotation
class WidgetText extends StatelessWidget {
WidgetText({
@required this.text,
this.subtitle,
});
final String text;
final String subtitle;
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(text),
if (subtitle != null) Text(subtitle),
],
);
}
}
Example of my visitor:
class MyAnnotationVisitor extends SimpleElementVisitor<void> {
@override
void visitFieldElement(FieldElement element) {
print('${element.name} is required? ${element.hasRequired}');
}
}
This visitor above output this for WidgetText
text is required? false
subtitle is required? false
The visitor can verify if the field is required only when an annotation is marked on the field this way below. But is repetitive and I would like to check in the constructor.
@myAnnotation
class WidgetText extends StatelessWidget {
WidgetText({
@required this.text,
this.subtitle,
});
@required
final String text;
final String subtitle;
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(text),
if (subtitle != null) Text(subtitle),
],
);
}
}