writeClassEncode method

  1. @override
void writeClassEncode(
  1. KotlinOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class klass,
  5. Set<String> customClassNames,
  6. Set<String> customEnumNames, {
  7. required String dartPackageName,
})
override

Writes a single class encode method to indent.

Implementation

@override
void writeClassEncode(
  KotlinOptions generatorOptions,
  Root root,
  Indent indent,
  Class klass,
  Set<String> customClassNames,
  Set<String> customEnumNames, {
  required String dartPackageName,
}) {
  indent.write('fun toList(): List<Any?> ');
  indent.addScoped('{', '}', () {
    indent.write('return listOf<Any?>');
    indent.addScoped('(', ')', () {
      for (final NamedType field in getFieldsInSerializationOrder(klass)) {
        final HostDatatype hostDatatype = _getHostDatatype(root, field);
        String toWriteValue = '';
        final String fieldName = field.name;
        final String safeCall = field.type.isNullable ? '?' : '';
        if (!hostDatatype.isBuiltin &&
            customClassNames.contains(field.type.baseName)) {
          toWriteValue = '$fieldName$safeCall.toList()';
        } else if (!hostDatatype.isBuiltin &&
            customEnumNames.contains(field.type.baseName)) {
          toWriteValue = '$fieldName$safeCall.raw';
        } else {
          toWriteValue = fieldName;
        }
        indent.writeln('$toWriteValue,');
      }
    });
  });
}