generateForAnnotatedElement method

  1. @override
FutureOr<String> generateForAnnotatedElement(
  1. Element element,
  2. ConstantReader ann,
  3. BuildStep buildStep
)

Implement to return source code to generate for element.

This method is invoked based on finding elements annotated with an instance of T. The annotation is provided as a ConstantReader.

Supported return values include a single String or multiple String instances within an Iterable or Stream. It is also valid to return a Future of String, Iterable, or Stream.

Implementations should return null when no content is generated. Empty or whitespace-only String instances are also ignored.

Implementation

@override
FutureOr<String> generateForAnnotatedElement(
  Element element,
  ConstantReader ann,
  BuildStep buildStep,
) {
  if (element is! ClassElement) return '';

  final className = element.name; // e.g. HomeScreen
  final libUri = element.librarySource.uri; // import path

  final overrideName = ann.peek('name')?.stringValue;
  final overridePath = ann.peek('path')?.stringValue;
  final fullscreenDialog = ann.peek('fullscreenDialog')?.boolValue ?? false;

  final routeName = overrideName ?? inferRouteClass(className); // HomeRoute
  final path = overridePath ?? inferPath(className); // /home

  _collector.add(
    _RouteMeta(
      importUri: libUri.toString(),
      className: className,
      routeName: routeName,
      path: path,
      fullscreenDialog: fullscreenDialog,
    ),
  );

  // ما نولّدش كود محلي هنا؛ التجميع حيتم في RoutesEmitter.
  return '';
}