writeFlutterApi method

  1. @override
void writeFlutterApi(
  1. KotlinOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. AstFlutterApi api, {
  5. required String dartPackageName,
})
override

Writes the code for a flutter Api, api. Example: class Foo(private val binaryMessenger: BinaryMessenger) { fun add(x: Int, y: Int, callback: (Int?) -> Unit) {...} }

Implementation

@override
void writeFlutterApi(
  KotlinOptions generatorOptions,
  Root root,
  Indent indent,
  AstFlutterApi api, {
  required String dartPackageName,
}) {
  final bool isCustomCodec = getCodecClasses(api, root).isNotEmpty;
  if (isCustomCodec) {
    _writeCodec(indent, api, root);
  }

  const List<String> generatedMessages = <String>[
    ' Generated class from Pigeon that represents Flutter messages that can be called from Kotlin.'
  ];
  addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
      generatorComments: generatedMessages);

  final String apiName = api.name;
  indent.write(
      'class $apiName(private val binaryMessenger: BinaryMessenger, private val messageChannelSuffix: String = "") ');
  indent.addScoped('{', '}', () {
    indent.write('companion object ');
    indent.addScoped('{', '}', () {
      indent.writeln('/** The codec used by $apiName. */');
      indent.write('val codec: MessageCodec<Any?> by lazy ');
      indent.addScoped('{', '}', () {
        if (isCustomCodec) {
          indent.writeln(_getCodecName(api));
        } else {
          indent.writeln('StandardMessageCodec()');
        }
      });
    });

    for (final Method method in api.methods) {
      _writeFlutterMethod(
        indent,
        generatorOptions: generatorOptions,
        name: method.name,
        parameters: method.parameters,
        returnType: method.returnType,
        channelName: makeChannelName(api, method, dartPackageName),
        documentationComments: method.documentationComments,
        dartPackageName: dartPackageName,
      );
    }
  });
}