writeFlutterApi method

  1. @override
void writeFlutterApi(
  1. KotlinOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Api 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,
  Api api, {
  required String dartPackageName,
}) {
  assert(api.location == ApiLocation.flutter);
  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.writeln('@Suppress("UNCHECKED_CAST")');
  indent
      .write('class $apiName(private val binaryMessenger: BinaryMessenger) ');
  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 func in api.methods) {
      final String channelName = makeChannelName(api, func, dartPackageName);
      final String returnType = func.returnType.isVoid
          ? ''
          : _nullsafeKotlinTypeForDartType(func.returnType);
      String sendArgument;

      addDocumentationComments(
          indent, func.documentationComments, _docCommentSpec);

      if (func.arguments.isEmpty) {
        indent.write('fun ${func.name}(callback: ($returnType) -> Unit) ');
        sendArgument = 'null';
      } else {
        final Iterable<String> argTypes = func.arguments
            .map((NamedType e) => _nullsafeKotlinTypeForDartType(e.type));
        final Iterable<String> argNames =
            indexMap(func.arguments, _getSafeArgumentName);
        sendArgument = 'listOf(${argNames.join(', ')})';
        final String argsSignature = map2(argTypes, argNames,
            (String type, String name) => '$name: $type').join(', ');
        if (func.returnType.isVoid) {
          indent.write(
              'fun ${func.name}($argsSignature, callback: () -> Unit) ');
        } else {
          indent.write(
              'fun ${func.name}($argsSignature, callback: ($returnType) -> Unit) ');
        }
      }
      indent.addScoped('{', '}', () {
        const String channel = 'channel';
        indent.writeln(
            'val $channel = BasicMessageChannel<Any?>(binaryMessenger, "$channelName", codec)');
        indent.write('$channel.send($sendArgument) ');
        if (func.returnType.isVoid) {
          indent.addScoped('{', '}', () {
            indent.writeln('callback()');
          });
        } else {
          indent.addScoped('{', '}', () {
            indent.writeln(
                'val result = ${_cast('it', type: func.returnType)}');
            indent.writeln('callback(result)');
          });
        }
      });
    }
  });
}