generate method

  1. @override
FutureOr<String> generate(
  1. LibraryReader library,
  2. BuildStep buildStep
)

Generates Dart code for an input Dart library.

May create additional outputs through the buildStep, but the 'primary' output is Dart code returned through the Future. If there is nothing to generate for this library may return null, or a Future that resolves to null or the empty string.

Implementation

@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
  Set<code_builder.Directive> import = {
    code_builder.Directive.import('dart:convert'),
    code_builder.Directive.import('package:http/http.dart', as: 'http')
  };

  final values = <String>{};

  for (var annotatedElement in library.annotatedWith(typeChecker)) {
    import.add(code_builder.Directive.import(
        annotatedElement.element.source!.shortName));
    values.addAll(import
        .map((e) => e.accept(code_builder.DartEmitter.scoped()).toString()));
    final generatedValue = generateForAnnotatedElement(
      annotatedElement.element,
      annotatedElement.annotation,
      buildStep,
    );

    await for (var value in normalizeGeneratorOutput(generatedValue)) {
      assert(value.length == value.trim().length);
      values.add(value);
    }
  }

  return values.join('\n\n');
}