generateModel method

String generateModel(
  1. String name
)

Generates a data model class with optional Freezed/Equatable integration.

name - The name of the model to generate.

Creates a model class in the data layer with:

  • JSON serialization support
  • Optional Freezed immutability (if enabled in config)
  • Optional Equatable value equality (if enabled in config)
  • Proper imports and annotations
  • Entity conversion methods

Implementation

String generateModel(String name) {
  final className = toPascalCase(name);

  if (config.useFreezed) {
    return '''
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../domain/entities/${toSnakeCase(name)}_entity.dart';

part '${toSnakeCase(name)}_model.freezed.dart';
part '${toSnakeCase(name)}_model.g.dart';

@freezed
class ${className}Model with _\$${className}Model {
const factory ${className}Model({
  required String id,
  // Add your model properties here
}) = _${className}Model;

factory ${className}Model.fromJson(Map<String, dynamic> json) => _\$${className}ModelFromJson(json);
}

extension ${className}ModelX on ${className}Model {
${className}Entity toEntity() {
  return ${className}Entity(
    id: id,
    // Map your properties here
  );
}
}
''';
  }

  return '''
import 'dart:convert';
import '../../domain/entities/${toSnakeCase(name)}_entity.dart';

class ${className}Model extends ${className}Entity {
const ${className}Model({
  required super.id,
  // Add your model properties here
});

factory ${className}Model.fromJson(Map<String, dynamic> json) {
  return ${className}Model(
    id: json['id'] ?? '',
    // Map your JSON properties here
  );
}

Map<String, dynamic> toJson() {
  return {
    'id': id,
    // Map your properties here
  };
}

factory ${className}Model.fromRawJson(String str) =>
    ${className}Model.fromJson(json.decode(str));

String toRawJson() => json.encode(toJson());
}
''';
}