generateModel method
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,
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(',')}];
}
''');
}