makeModel static method
Creates a new Model.
Implementation
static Future<void> makeModel(
String className,
String value, {
String folderPath = modelsFolder,
bool forceCreate = false,
bool addToConfig = true,
bool? skipIfExist = false,
String? creationPath,
}) async {
String name = className.replaceAll(RegExp(r'(_?model)'), "");
ReCase nameReCase = ReCase(name);
// create missing directories in the project
await _makeDirectory(folderPath);
await createDirectoriesFromCreationPath(creationPath, folderPath);
// create file path
String filePath = createPathForDartFile(
folderPath: folderPath,
className: name,
creationPath: creationPath,
);
if (skipIfExist == true) {
if (await hasFile(filePath)) return;
}
await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);
await _createNewFile(
filePath,
value,
onSuccess: () {
final String linkText = nameReCase.snakeCase;
final String link = MetroConsole.hyperlink(linkText, filePath);
MetroConsole.writeInGreen('[Model] $link created 🎉');
},
);
if (addToConfig == false) return;
String classImport = makeImportPathModel(
nameReCase.snakeCase,
creationPath: creationPath ?? "",
);
await MetroService.addToConfig(
configName: "decoders",
classImport: classImport,
createTemplate: (file) {
String modelName = nameReCase.pascalCase;
if (usesDecodersList(file)) {
return addToDecodersList(file, ' Decode($modelName.fromJson),\n') ??
file;
}
return addToModelDecoders(file, """
List<$modelName>: (data) => List.from(data).map((json) => $modelName.fromJson(json)).toList(),
$modelName: (data) => $modelName.fromJson(data),
""") ??
file;
},
);
}