So I've been playing around with code generation in Dart, and I have a problem resolving Element type in "data" classes. I have 2 data classes, and 2nd one has a type of a 1st one defined as a field.
first.dart:
part 'first.g.dart';
@Data(name="First")
class _First {
}
second.dart:
import 'first.dart';
part 'second.g.dart';
@Data(name="Second")
class _Second {
First first;
}
You might have noticed, that _Second has field First instead of _First. That's because I generate two new classes. These two new classes contain additional functionality, like generated hashCode and some other custom methods I need.
In my Generator class, I need to obtain type string of field first. I'm doing this by fieldElement.type?.getDisplayString().
This works fine for all other non-generated classes, like num for example.
But in case of first field, returned type string is "dynamic".
Is there a way to obtain correct type? Should "First" in this case.
data_generator.dart
class Data extends GeneratorForAnnotation<Data> {
@override
generateForAnnotatedElement(Element element, ConstantReader annotation,
BuildStep buildStep) {
if(element is ClassElement) {
FieldElement element = findField();
String type = element.type?.getDisplayString();
assert(type != "dynamic");
//...
}
}
//...
}