getAnnotation method

DartObject? getAnnotation(
  1. Element element
)

Returns the annotation of type AnnotationType of the given element, or null if it doesn't have any.

Implementation

DartObject? getAnnotation(Element element) {
  final annotations = TypeChecker.fromRuntime(AnnotationType).annotationsOf(element);
  if (annotations.isEmpty) {
    return null;
  }
  if (annotations.length > 1) {
    throw Exception('You tried to add multiple @$AnnotationType() annotations to the '
        "same element (${element.name}), but that's not possible.");
  }
  return annotations.single;
}