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 _ObjcType returnTypeName = _objcTypeForDartType(
      generatorOptions.prefix,
      func.returnType,
      // Nullability is required since the return must be nil if NSError is
      // set.
      forceNullability: true,
    );

    String? lastArgName;
    String? lastArgType;
    String? returnType;
    final String enumReturnType = _enumName(
      func.returnType.baseName,
      suffix: ' *_Nullable',
      prefix: generatorOptions.prefix,
      box: true,
    );
    if (func.isAsynchronous) {
      returnType = 'void';
      lastArgName = 'completion';
      if (func.returnType.isVoid) {
        lastArgType = 'void (^)(FlutterError *_Nullable)';
      } else if (func.returnType.isEnum) {
        lastArgType = 'void (^)($enumReturnType, FlutterError *_Nullable)';
      } else {
        lastArgType =
            'void (^)(${returnTypeName.beforeString}_Nullable, FlutterError *_Nullable)';
      }
    } else {
      if (func.returnType.isVoid) {
        returnType = 'void';
      } else if (func.returnType.isEnum) {
        returnType = enumReturnType;
      } else {
        returnType = 'nullable $returnTypeName';
      }

      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,
    );
    indent.writeln('$signature;');
  }
  indent.writeln('@end');
  indent.newln();
  indent.writeln(
      'extern void SetUp$apiName(id<FlutterBinaryMessenger> binaryMessenger, NSObject<$apiName> *_Nullable api);');
  indent.newln();
  indent.writeln(
      'extern void SetUp${apiName}WithSuffix(id<FlutterBinaryMessenger> binaryMessenger, NSObject<$apiName> *_Nullable api, NSString *messageChannelSuffix);');
  indent.newln();
}