writeFlutterApi method
void
writeFlutterApi(
- SwiftOptions generatorOptions,
- Root root,
- Indent indent,
- AstFlutterApi api, {
- 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,
}) {
final bool isCustomCodec = getCodecClasses(api, root).isNotEmpty;
if (isCustomCodec) {
_writeCodec(indent, api, root);
}
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(api);
String codecArgumentString = '';
if (getCodecClasses(api, root).isNotEmpty) {
codecArgumentString = ', codec: codec';
indent.write('var codec: FlutterStandardMessageCodec ');
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,
codecArgumentString: codecArgumentString,
swiftFunction: func.swiftFunction,
);
}
});
}