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,
}) {
  indent.writeln(
      '$_docCommentPrefix The codec used by ${_className(generatorOptions.prefix, api.name)}.');
  indent.writeln(
      'NSObject<FlutterMessageCodec> *${_getCodecGetterName(generatorOptions.prefix, api.name)}(void);');
  indent.newln();
  final String apiName = _className(generatorOptions.prefix, api.name);
  addDocumentationComments(
      indent, api.documentationComments, _docCommentSpec);

  indent.writeln('@protocol $apiName');
  for (final Method func in api.methods) {
    final _ObjcPtr returnTypeName =
        _objcTypeForDartType(generatorOptions.prefix, func.returnType);

    String? lastArgName;
    String? lastArgType;
    String? returnType;
    if (func.isAsynchronous) {
      returnType = 'void';
      if (func.returnType.isVoid) {
        lastArgType = 'void (^)(FlutterError *_Nullable)';
        lastArgName = 'completion';
      } else {
        lastArgType =
            'void (^)(${returnTypeName.ptr}_Nullable, FlutterError *_Nullable)';
        lastArgName = 'completion';
      }
    } else {
      returnType = func.returnType.isVoid
          ? 'void'
          : 'nullable ${returnTypeName.ptr.trim()}';
      lastArgType = 'FlutterError *_Nullable *_Nonnull';
      lastArgName = 'error';
    }
    final List<String> generatorComments = <String>[];
    if (!func.returnType.isNullable &&
        !func.returnType.isVoid &&
        !func.isAsynchronous) {
      generatorComments.add(' @return `nil` only when `error != nil`.');
    }
    addDocumentationComments(
        indent, func.documentationComments, _docCommentSpec,
        generatorComments: generatorComments);

    final String signature = _makeObjcSignature(
      func: func,
      options: generatorOptions,
      returnType: returnType,
      lastArgName: lastArgName,
      lastArgType: lastArgType,
      isEnum: (TypeDeclaration t) => isEnum(root, t),
    );
    indent.writeln('$signature;');
  }
  indent.writeln('@end');
  indent.newln();
  indent.writeln(
      'extern void ${apiName}Setup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<$apiName> *_Nullable api);');
  indent.newln();
}