toDartType method
Return a proper Dart type for this schema
Implementation
String toDartType({
Map<String, List<String>>? unions,
}) {
return map(
object: (s) {
if (s.anyOf != null && unions != null) {
final subSchemas = s.anyOf!.map((e) => e.toDartType()).toList();
final leq = ListEquality();
for (final e in unions.entries) {
if (leq.equals(subSchemas, e.value)) {
final type = e.key.pascalCase;
if (s.nullable == true) {
return '$type?';
} else {
return type;
}
}
}
} else if (s.ref != null) {
String type;
// Do not modify all uppercase schema names
if (s.ref == s.ref!.toUpperCase()) {
type = s.ref!;
} else {
type = s.ref!.pascalCase;
}
if (s.nullable == true) {
return '$type?';
} else {
return type;
}
} else if (s.properties != null || s.anyOf != null) {
return 'Map<String,dynamic>';
}
return 'dynamic';
},
boolean: (s) {
return s.nullable == true ? 'bool?' : 'bool';
},
string: (s) {
return s.nullable == true ? 'String?' : 'String';
},
integer: (s) {
return s.nullable == true ? 'int?' : 'int';
},
number: (s) {
return s.nullable == true ? 'double?' : 'double';
},
enumeration: (s) {
return s.ref ?? 'String';
},
array: (s) {
final itemType = s.items.toDartType();
return s.nullable == true ? 'List<$itemType>?' : 'List<$itemType>';
},
map: (s) {
String valueType = s.valueSchema?.toDartType() ?? 'dynamic';
if (valueType != 'dynamic' && s.valueSchema?.nullable == true) {
valueType = '$valueType?';
}
return s.nullable == true
? 'Map<String,$valueType>?'
: 'Map<String,$valueType>';
},
);
}