writeHostApi method

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

Writes a single Host Api to indent.

Implementation

@override
void writeHostApi(
  ObjcOptions generatorOptions,
  Root root,
  Indent indent,
  Api api, {
  required String dartPackageName,
}) {
  assert(api.location == ApiLocation.host);
  final String apiName = _className(generatorOptions.prefix, api.name);

  _writeCodecAndGetter(generatorOptions, root, indent, api);

  const String channelName = 'channel';
  indent.write(
      'void ${apiName}Setup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<$apiName> *api) ');
  indent.addScoped('{', '}', () {
    for (final Method func in api.methods) {
      addDocumentationComments(
          indent, func.documentationComments, _docCommentSpec);

      indent.writeScoped('{', '}', () {
        String? taskQueue;
        if (func.taskQueueType != TaskQueueType.serial) {
          taskQueue = 'taskQueue';
          indent.writeln(
              'NSObject<FlutterTaskQueue> *$taskQueue = [binaryMessenger makeBackgroundTaskQueue];');
        }
        _writeChannelAllocation(
          generatorOptions,
          indent,
          api,
          func,
          channelName,
          taskQueue,
          dartPackageName: dartPackageName,
        );
        indent.write('if (api) ');
        indent.addScoped('{', '}', () {
          _writeChannelApiBinding(
              generatorOptions, root, indent, apiName, func, channelName);
        }, addTrailingNewline: false);
        indent.add(' else ');
        indent.addScoped('{', '}', () {
          indent.writeln('[$channelName setMessageHandler:nil];');
        });
      });
    }
  });
}