Test Annotation

This commit is contained in:
Thomas Hii 2023-01-29 14:54:00 +00:00
parent ec68a78c1d
commit 342a5a4ac9
2 changed files with 17 additions and 3 deletions

View file

@ -1,4 +1,5 @@
import 'dart:mirrors'; import 'dart:mirrors';
import 'package:example1/src/models.dart';
void main() { void main() {
final stopwatch = Stopwatch()..start(); final stopwatch = Stopwatch()..start();
@ -14,15 +15,23 @@ void main() {
printAnnotationValue(String); printAnnotationValue(String);
printAnnotationValue(Shape); printAnnotationValue(Shape);
printAnnotationValue(Square);
} }
class Shape { class Shape {
void draw() => print("Draw Shape"); void draw() => print("Draw Shape");
} }
@Person('Will', 'Tom')
class Square {
void greetHii() {
print("Hii Welcome to flutter agency");
}
}
void printAnnotationValue(final Type clazz) { void printAnnotationValue(final Type clazz) {
final DeclarationMirror clazzDeclaration = reflectClass(clazz); final DeclarationMirror clazzDeclaration = reflectClass(clazz);
final ClassMirror someAnnotationMirror = reflectClass(Shape); final ClassMirror someAnnotationMirror = reflectClass(Person);
final annotationInstsanceMirror = final annotationInstsanceMirror =
clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror); clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror);
if (annotationInstsanceMirror.isEmpty) { if (annotationInstsanceMirror.isEmpty) {
@ -30,6 +39,6 @@ void printAnnotationValue(final Type clazz) {
return; return;
} }
final someAnnotationInstance = final someAnnotationInstance =
(annotationInstsanceMirror.first.reflectee as Shape); (annotationInstsanceMirror.first.reflectee as Person);
print("${someAnnotationInstance.draw}"); print(someAnnotationInstance.firstName);
} }

View file

@ -0,0 +1,5 @@
class Person {
final String firstName;
final String lastName;
const Person(this.firstName, this.lastName);
}