writeDataClass method

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

Writes a single data class to indent.

Implementation

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

  addDocumentationComments(
      indent, klass.documentationComments, _docCommentSpec);

  indent.writeln('@interface ${_className(prefix, klass.name)} : NSObject');
  if (getFieldsInSerializationOrder(klass).isNotEmpty) {
    if (getFieldsInSerializationOrder(klass)
        .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, klass, classes, enums, prefix);
    indent.addln(';');
  }
  for (final NamedType field in getFieldsInSerializationOrder(klass)) {
    final HostDatatype hostDatatype = getFieldHostDatatype(
        field,
        classes,
        enums,
        (TypeDeclaration x) => _objcTypePtrForPrimitiveDartType(prefix, x),
        customResolver: customEnumNames.contains(field.type.baseName)
            ? (String x) => _className(prefix, x)
            : (String x) => '${_className(prefix, x)} *');
    late final String propertyType;
    addDocumentationComments(
        indent, field.documentationComments, _docCommentSpec);
    if (customEnumNames.contains(field.type.baseName)) {
      propertyType = 'assign';
    } else {
      propertyType = _propertyTypeForDartType(field);
    }
    final String nullability =
        _isNullable(hostDatatype, field.type) ? ', nullable' : '';
    indent.writeln(
        '@property(nonatomic, $propertyType$nullability) ${hostDatatype.datatype} ${field.name};');
  }
  indent.writeln('@end');
  indent.newln();
}