generateModel method

void generateModel({
  1. required String path,
  2. required String className,
  3. List<String> importForeign = const [],
  4. required List<String> constructor,
  5. required List<String> constructorAssign,
  6. required List<String> variables,
  7. required List<String> toMap,
  8. required List<String> fromMap,
  9. List<String> fromMapWithJoin = const [],
  10. required List<String> paramCopyWith,
  11. required List<String> valueCopyWith,
  12. required List<String> props,
})

Implementation

void generateModel({
  required String path,
  required String className,
  List<String> importForeign = const [],
  required List<String> constructor,
  required List<String> constructorAssign,
  required List<String> variables,
  required List<String> toMap,
  required List<String> fromMap,
  List<String> fromMapWithJoin = const [],
  required List<String> paramCopyWith,
  required List<String> valueCopyWith,
  required List<String> props,
}) {
  path.write('''import 'dart:convert';

import 'package:equatable/equatable.dart';

${importForeign.join('\n')}

class ${className.toString().pascalCase} extends Equatable {
const ${className.toString().pascalCase}({
  ${constructor.join('\n')}
}) ${constructorAssign.isNotEmpty ? ': ${constructorAssign.join(',')}' : ''};

${variables.join('\n')}

Map<String, dynamic> toMap({bool withJoin = false}) {
  return {
    ${toMap.join('\n')}
  };
}

factory ${className.toString().pascalCase}.fromMap(Map<String, dynamic> map, {bool withJoin = false,}) {
  return ${className.toString().pascalCase}(
    ${fromMap.join('\n')}
  );
}

${fromMapWithJoin.isEmpty ? '' : '''factory ${className.toString().pascalCase}.fromMapWithJoin(Map<String, dynamic> map, {bool withJoin = false,}) {
  return ${className.toString().pascalCase}(
     ${fromMapWithJoin.join('\n')}
  );
}'''}

String toJson({bool withJoin = false}) => json.encode(toMap(withJoin: withJoin));

factory ${className.toString().pascalCase}.fromJson(String source) =>
    ${className.toString().pascalCase}.fromMap(json.decode(source));

${className.toString().pascalCase} copyWith({
  ${paramCopyWith.join('\n')}
}) {
  return ${className.toString().pascalCase}(
    ${valueCopyWith.join('\n')}
  );
}

@override
List<Object?> get props => [${props.join(',')}];
}
''');
}