writeHostApi method

  1. @override
void writeHostApi(
  1. DartOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. AstHostApi api, {
  5. required String dartPackageName,
})
override

Writes the code for host Api, api. Example: class FooCodec extends StandardMessageCodec {...}

class Foo { Foo(BinaryMessenger? binaryMessenger) {} static const MessageCodec<Object?> codec = FooCodec(); Future

Messages will be sent and received in a list.

If the message received was successful, the result will be contained at the 0'th index.

If the message was a failure, the list will contain 3 items: a code, a message, and details in that order.

Implementation

@override
void writeHostApi(
  DartOptions generatorOptions,
  Root root,
  Indent indent,
  AstHostApi api, {
  required String dartPackageName,
}) {
  indent.newln();
  bool first = true;
  addDocumentationComments(
      indent, api.documentationComments, _docCommentSpec);
  indent.write('class ${api.name} ');
  indent.addScoped('{', '}', () {
    indent.format('''
/// Constructor for [${api.name}].  The [binaryMessenger] named argument is
/// available for dependency injection.  If it is left null, the default
/// BinaryMessenger will be used which routes to the host platform.
${api.name}({BinaryMessenger? binaryMessenger, String messageChannelSuffix = ''})
  : ${varNamePrefix}binaryMessenger = binaryMessenger,
    ${varNamePrefix}messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.\$messageChannelSuffix' : '';
final BinaryMessenger? ${varNamePrefix}binaryMessenger;
''');

    indent.writeln(
        'static const MessageCodec<Object?> $_pigeonChannelCodec = $_pigeonCodec();');
    indent.newln();
    indent.writeln('final String $_suffixVarName;');
    indent.newln();
    for (final Method func in api.methods) {
      if (!first) {
        indent.newln();
      } else {
        first = false;
      }
      _writeHostMethod(
        indent,
        name: func.name,
        parameters: func.parameters,
        returnType: func.returnType,
        documentationComments: func.documentationComments,
        channelName: makeChannelName(api, func, dartPackageName),
        addSuffixVariable: true,
      );
    }
  });
}