createClass method
Generates the complete Dart class code.
Creates a full Dart class with:
- Constructor with required/optional parameters
- Property declarations with proper types and nullability
fromJson
factory constructor for JSON deserialization
Returns the complete class code as a string, or empty string if the table name is invalid for Dart class generation.
Implementation
String createClass() {
final buffer = StringBuffer();
if (hasValidName) {
buffer.writeln('class $className {');
// Generate constructors
buffer.writeln(' $className({');
for (final prop in properties) {
buffer.writeln(
' ${prop.isNullable ? "" : "required"} this.${prop.dartName},',
);
}
buffer.writeln('});');
// Genereate properties
for (final prop in properties) {
buffer.writeln(prop.field);
}
buffer.writeln(fromJsonFunction);
buffer.writeln('}');
buffer.writeCharCode("\n".codeUnitAt(0));
}
return buffer.toString();
}