textToFunctionDart function
String
textToFunctionDart(
{ - required String key,
- required dynamic value,
- required String returnType,
- required String className,
- bool isClass = false,
- bool isList = false,
- required void paramFunction(
- String text
),
- required void paramJson(
- String text
),
})
Implementation
String textToFunctionDart({
required String key,
required dynamic value,
required String returnType,
required String className,
bool isClass = false,
bool isList = false,
String? comment,
required void Function(String text) paramFunction,
required void Function(String text) paramJson,
}) {
comment ??= "";
String nameClass = className.camelCaseClass();
String nameMethod = key
.replaceAll(RegExp(r"^(@|[0-9]+)", caseSensitive: false), "special_")
.replaceAll(RegExp(r"([\-]+)", caseSensitive: false), "_")
.replaceAll(RegExp(r"^(_)", caseSensitive: false), "special_");
if (RegExp(r"^(do|is|in)$", caseSensitive: false).hasMatch(nameMethod)) {
nameMethod += "_";
}
if (isClass) {
if (isList) {
paramFunction.call("""
List<${nameClass}>? ${nameMethod.toLowerCase()},
""");
// old
// paramJson.call("""
// "${key}": (${nameMethod} != null)? ${nameMethod}.map((res) => res.toJson()).toList().cast<Map>(): null,
// """);
paramJson.call("""
"${key}": (${nameMethod.toLowerCase()} != null)? ${nameMethod.toLowerCase()}.toJson(): null,
""");
return """
${comment}
List<${nameClass}> get ${nameMethod.toLowerCase()} {
try {
if (rawData["${key}"] is List == false){
return [];
}
return (rawData["${key}"] as List).map((e) => ${nameClass}(e as ${returnType})).toList().cast<${nameClass}>();
} catch (e) {
return [];
}
}
${comment}
set ${nameMethod.toLowerCase()}(List<${nameClass}> values) {
rawData["${key}"] = values.map((value) => value.toJson()).toList();
}
""";
}
paramFunction.call("""
${nameClass}? ${nameMethod.toLowerCase()},
""");
paramJson.call("""
"${key}": (${nameMethod.toLowerCase()} != null)?${nameMethod.toLowerCase()}.toJson(): null,
""");
return """
${comment}
${nameClass} get ${nameMethod.toLowerCase()} {
try {
if (rawData["${key}"] is ${returnType} == false){
return ${nameClass}({});
}
return ${nameClass}(rawData["${key}"] as ${returnType});
} catch (e) {
return ${nameClass}({});
}
}
${comment}
set ${nameMethod.toLowerCase()}(${nameClass} value) {
rawData["${key}"] = value.toJson();
}
""";
}
if (isList) {
paramFunction.call("""
List<${returnType}>? ${nameMethod.toLowerCase()},
""");
paramJson.call("""
"${key}": ${nameMethod.toLowerCase()},
""");
return """
${comment}
///
/// default:
///
///
List<${returnType}> get ${nameMethod.toLowerCase()} {
try {
if (rawData["${key}"] is List == false){
return [];
}
return (rawData["${key}"] as List).cast<${returnType}>();
} catch (e) {
return [];
}
}
${comment}
set ${nameMethod.toLowerCase()}(List<${returnType}> value) {
rawData["${key}"] = value;
}
""";
}
if (RegExp(r"^(special_)", caseSensitive: false).hashData(nameMethod)) {
paramFunction.call("""
${returnType} ${nameMethod.toLowerCase()} = ${json.encode(value)},
""");
} else {
paramFunction.call("""
${returnType}? ${nameMethod.toLowerCase()},
""");
}
paramJson.call("""
"${key}": ${nameMethod.toLowerCase()},
""");
return """
${comment}
${returnType}? get ${nameMethod.toLowerCase()} {
try {
if (rawData["${key}"] is ${returnType} == false){
return null;
}
return rawData["${key}"] as ${returnType};
} catch (e) {
return null;
}
}
${comment}
set ${nameMethod.toLowerCase()}(${returnType}? value) {
rawData["${key}"] = value;
}
""";
}