writeFlutterApi method

  1. @override
void writeFlutterApi(
  1. SwiftOptions 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 let binaryMessenger: FlutterBinaryMessenger init(binaryMessenger: FlutterBinaryMessenger) {...} func add(x: Int32, y: Int32, completion: @escaping (Int32?) -> Void) {...} }

Implementation

@override
void writeFlutterApi(
  SwiftOptions generatorOptions,
  Root root,
  Indent indent,
  AstFlutterApi api, {
  required String dartPackageName,
}) {
  const List<String> generatedComments = <String>[
    ' Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.'
  ];
  addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
      generatorComments: generatedComments);

  indent.addScoped('protocol ${api.name}Protocol {', '}', () {
    for (final Method func in api.methods) {
      addDocumentationComments(
          indent, func.documentationComments, _docCommentSpec);
      indent.writeln(_getMethodSignature(
        name: func.name,
        parameters: func.parameters,
        returnType: func.returnType,
        errorTypeName: _getErrorClassName(generatorOptions),
        isAsynchronous: true,
        swiftFunction: func.swiftFunction,
        getParameterName: _getSafeArgumentName,
      ));
    }
  });

  indent.write('class ${api.name}: ${api.name}Protocol ');
  indent.addScoped('{', '}', () {
    indent.writeln('private let binaryMessenger: FlutterBinaryMessenger');
    indent.writeln('private let messageChannelSuffix: String');
    indent.write(
        'init(binaryMessenger: FlutterBinaryMessenger, messageChannelSuffix: String = "") ');
    indent.addScoped('{', '}', () {
      indent.writeln('self.binaryMessenger = binaryMessenger');
      indent.writeln(
          r'self.messageChannelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""');
    });
    final String codecName = _getCodecName(generatorOptions);
    indent.write('var codec: $codecName ');
    indent.addScoped('{', '}', () {
      indent.writeln('return $codecName.shared');
    });

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