getVariableFromMap method
String
getVariableFromMap(
- String key,
- dynamic value,
- String suffix,
- List<
ModelClassName> listClassName, - String parent,
Implementation
String getVariableFromMap(String key, dynamic value, String suffix,
List<ModelClassName> listClassName, String parent) {
final variable = "map['$key']";
if (value is int) {
return "int.tryParse($variable?.toString() ?? '')";
}
if (value is double) {
return "double.tryParse($variable?.toString() ?? '')";
}
if (value is bool) {
return variable;
}
if (value is Map) {
final data =
'${ModelClassNameHelper.getClassName(listClassName, suffix, key.pascalCase, false, false, parent)}.fromMap($variable,)';
return '$variable == null ? null : $data';
}
if (value is List) {
if (value.isNotEmpty) {
if (value.first is Map) {
final data =
'List.from(($variable as List).where((element) => element != null).map((e) => ${ModelClassNameHelper.getClassName(listClassName, suffix, key.pascalCase, false, false, parent)}.fromMap(e)),)';
return '$variable is List ? $data : null';
} else if (value.first is String &&
RegExp(r'^\d{4}-\d{2}-\d{2}(\s|T)?(\d{2}:\d{2}(:\d{2})?)?(\.\d+)?Z?$')
.hasMatch(value.first)) {
final data =
'List.from(($variable as List).where((element) => element != null).map((e) => DateTime.parse($variable)}.fromMap(e)),)';
return '$variable is List ? $data : null';
} else {
final data = 'List.from($variable)';
return '$variable is List ? $data : null';
}
}
}
if (value is String) {
if (RegExp(r'^\d{4}-\d{2}-\d{2}(\s|T)?(\d{2}:\d{2}(:\d{2})?)?(\.\d+)?Z?$')
.hasMatch(value)) {
return "DateTime.tryParse($variable ?? '')";
} else {
return variable;
}
}
return variable;
}