textToListFunction function
String
textToListFunction(
{ - required String key,
- required String returnType,
- required String className,
- bool isClass = false,
- bool isList = false,
- required void paramFunction(
- String text
),
- required void paramJson(
- String text
),
})
Implementation
String textToListFunction({
required String key,
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<List<${nameClass}>>? ${nameMethod.toLowerCase()},
""");
paramJson.call("""
"${key}": (${nameMethod.toLowerCase()} != null)? ${nameMethod.toLowerCase()}.map((res) => res.map((e) => e.toJson()).toList().cast<Map>()).toList().cast<List<Map>>(): null,
""");
return """
${comment}
List<List<${nameClass}>> get ${nameMethod.toLowerCase()} {
try {
if (rawData["${key}"] is List == false){
return [];
}
return ((rawData["${key}"] as List).cast<List>()).map((e) => e.map((e) => ${nameClass}(e as Map)).toList().cast<${nameClass}>()).toList().cast<List<${nameClass}>>();
} catch (e) {
return [];
}
}
${comment}
set ${nameMethod.toLowerCase()}(List<List<${nameClass}>> values) {
rawData["${key}"] = values.map((value) => value.map((value) => value.toJson()).toList()).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<List<${returnType}>>? ${nameMethod.toLowerCase()},
""");
paramJson.call("""
"${key}": ${nameMethod.toLowerCase()},
""");
return """
${comment}
List<List<${returnType}>> get ${nameMethod.toLowerCase()} {
try {
if (rawData["${key}"] is List == false){
return [];
}
return (rawData["${key}"] as List).cast<List<${returnType}>>();
} catch (e) {
return [];
}
}
${comment}
set ${nameMethod.toLowerCase()}(List<List<${returnType}>> value) {
rawData["${key}"] = value;
}
""";
}
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;
}
""";
}