writeDataClass method

  1. @override
void writeDataClass(
  1. ObjcOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class classDefinition, {
  5. required String dartPackageName,
})
override

Writes a single data class to indent.

Implementation

@override
void writeDataClass(
  ObjcOptions generatorOptions,
  Root root,
  Indent indent,
  Class classDefinition, {
  required String dartPackageName,
}) {
  final List<Class> classes = root.classes;
  final List<Enum> enums = root.enums;
  final String? prefix = generatorOptions.prefix;

  addDocumentationComments(
      indent, classDefinition.documentationComments, _docCommentSpec);

  indent.writeln(
      '@interface ${_className(prefix, classDefinition.name)} : NSObject');
  if (getFieldsInSerializationOrder(classDefinition).isNotEmpty) {
    if (getFieldsInSerializationOrder(classDefinition)
        .map((NamedType e) => !e.type.isNullable)
        .any((bool e) => e)) {
      indent.writeln(
          '$_docCommentPrefix `init` unavailable to enforce nonnull fields, see the `make` class method.');
      indent.writeln('- (instancetype)init NS_UNAVAILABLE;');
    }
    _writeObjcSourceClassInitializerDeclaration(
      indent,
      generatorOptions,
      root,
      classDefinition,
      classes,
      enums,
      prefix,
    );
    indent.addln(';');
  }
  for (final NamedType field
      in getFieldsInSerializationOrder(classDefinition)) {
    final HostDatatype hostDatatype = getFieldHostDatatype(
        field,
        (TypeDeclaration x) => _objcTypeStringForPrimitiveDartType(prefix, x,
            beforeString: true),
        customResolver: field.type.isEnum
            ? (String x) => _enumName(x, prefix: prefix)
            : (String x) => '${_className(prefix, x)} *');
    late final String propertyType;
    addDocumentationComments(
        indent, field.documentationComments, _docCommentSpec);
    propertyType = _propertyTypeForDartType(field.type,
        isNullable: field.type.isNullable, isEnum: field.type.isEnum);
    final String nullability = field.type.isNullable ? ', nullable' : '';
    final String fieldType = field.type.isEnum && field.type.isNullable
        ? _enumName(field.type.baseName,
            suffix: ' *',
            prefix: generatorOptions.prefix,
            box: field.type.isNullable)
        : hostDatatype.datatype;
    indent.writeln(
        '@property(nonatomic, $propertyType$nullability) $fieldType ${field.name};');
  }
  indent.writeln('@end');
  indent.newln();
}