writeFlutterApi method

  1. @override
void writeFlutterApi(
  1. DartOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. AstFlutterApi api, {
  5. String channelNameFunc(
    1. Method
    )?,
  6. bool isMockHandler = false,
  7. required String dartPackageName,
})
override

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

abstract class Foo { static const MessageCodec<Object?> codec = FooCodec(); int add(int x, int y); static void setUp(Foo api, {BinaryMessenger? binaryMessenger}) {...} }

Implementation

@override
void writeFlutterApi(
  DartOptions generatorOptions,
  Root root,
  Indent indent,
  AstFlutterApi api, {
  String Function(Method)? channelNameFunc,
  bool isMockHandler = false,
  required String dartPackageName,
}) {
  indent.newln();
  addDocumentationComments(
      indent, api.documentationComments, _docCommentSpec);

  indent.write('abstract class ${api.name} ');
  indent.addScoped('{', '}', () {
    if (isMockHandler) {
      indent.writeln(
          'static TestDefaultBinaryMessengerBinding? get _testBinaryMessengerBinding => TestDefaultBinaryMessengerBinding.instance;');
    }
    indent.writeln(
        'static const MessageCodec<Object?> $_pigeonChannelCodec = $_pigeonCodec();');
    indent.newln();
    for (final Method func in api.methods) {
      addDocumentationComments(
          indent, func.documentationComments, _docCommentSpec);

      final bool isAsync = func.isAsynchronous;
      final String returnType = isAsync
          ? 'Future<${_addGenericTypesNullable(func.returnType)}>'
          : _addGenericTypesNullable(func.returnType);
      final String argSignature =
          _getMethodParameterSignature(func.parameters);
      indent.writeln('$returnType ${func.name}($argSignature);');
      indent.newln();
    }
    indent.write(
        "static void setUp(${api.name}? api, {BinaryMessenger? binaryMessenger, String messageChannelSuffix = '',}) ");
    indent.addScoped('{', '}', () {
      indent.writeln(
          r"messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.$messageChannelSuffix' : '';");

      for (final Method func in api.methods) {
        _writeFlutterMethodMessageHandler(
          indent,
          name: func.name,
          parameters: func.parameters,
          returnType: func.returnType,
          addSuffixVariable: true,
          channelName: channelNameFunc == null
              ? makeChannelName(api, func, dartPackageName)
              : channelNameFunc(func),
          isMockHandler: isMockHandler,
          isAsynchronous: func.isAsynchronous,
        );
      }
    });
  });
}