basico_model.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /// Modelo de datos para retener la informacion basica necesaria de todos los modelos
  2. class Basico {
  3. static const String identificadorLocal = "idLocal";
  4. static const String identificadorWeb = "id";
  5. int id;
  6. int idLocal;
  7. DateTime? creado;
  8. DateTime? modificado;
  9. DateTime? eliminado;
  10. Basico({
  11. this.id = 0,
  12. this.idLocal = -1,
  13. this.creado,
  14. this.modificado,
  15. this.eliminado,
  16. });
  17. static parseDate(origen) {
  18. if (origen == null || origen == "null" || origen == '') {
  19. return null;
  20. }
  21. try {
  22. return DateTime.parse(origen);
  23. } catch (e) {
  24. print("Error al parsear fecha: $e");
  25. return null;
  26. }
  27. }
  28. static double parseDouble(origen) {
  29. if (origen == null) return 0.0;
  30. if (origen == "null") return 0.0;
  31. if (origen == "") return 0.0;
  32. if (origen.runtimeType.toString() == 'double') return origen;
  33. return double.parse(origen.toString());
  34. }
  35. static int parseInt(origen) {
  36. if (origen == null) return 0;
  37. if (origen == "") return 0;
  38. if (origen == "null") return 0;
  39. if (origen.runtimeType.toString() == 'int') return origen;
  40. return int.parse(origen.toString());
  41. }
  42. static String parseString(origen) {
  43. if (origen == "null") return "";
  44. if (origen == null) return "";
  45. if (origen == "") return "";
  46. if (origen == "null") return "";
  47. if (origen.runtimeType.toString() == 'string') return origen;
  48. return origen.toString();
  49. }
  50. static bool parseBolean(origen) {
  51. if (origen == null) return false;
  52. if (origen == "") return false;
  53. if (origen == "null") return false;
  54. if (origen == "false") return false;
  55. if (origen.runtimeType.toString() == 'bool') return origen;
  56. return bool.parse(origen.toString());
  57. }
  58. Map<String, dynamic> toJson() {
  59. return {
  60. 'id': id,
  61. 'idLocal': idLocal,
  62. 'eliminado': eliminado,
  63. };
  64. }
  65. parseJson(Map<String, dynamic> json) {
  66. id = json['id'] != null ? int.parse(json['id'].toString()) : 0;
  67. idLocal = json['idLocal'] ?? -1;
  68. creado = json['creado'] != null ? parseDate(json['creado']) : creado;
  69. modificado =
  70. json['modificado'] != null ? parseDate(json['modificado']) : modificado;
  71. eliminado =
  72. json['eliminado'] != null ? parseDate(json['eliminado']) : eliminado;
  73. }
  74. }