writeFlutterApi method
void
writeFlutterApi(
- DartOptions generatorOptions,
- Root root,
- Indent indent,
- AstFlutterApi api, {
- String channelNameFunc()?,
- bool isMockHandler = false,
- 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,
}) {
String codecName = _standardMessageCodec;
if (getCodecClasses(api, root).isNotEmpty) {
codecName = _getCodecName(api);
_writeCodec(indent, codecName, api, root);
}
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 = $codecName();');
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,
);
}
});
});
}