// lib/src/models/category_model.dart class CategoriaModel { int id; int? version; String? key; String? name; int? createdDate; CategoriaModel? parentCategory; List? subCategories; bool? active; CategoriaModel({ required this.id, this.version, this.key, this.name, this.createdDate, this.parentCategory, this.subCategories, this.active, }); // Método para crear una instancia de CategoryModel desde un JSON factory CategoriaModel.fromJson(Map json) { return CategoriaModel( id: json['id'], version: json['version'], key: json['clave'], name: json['nombre'], createdDate: json['fechaCreado'], parentCategory: json['categoria'] != null ? CategoriaModel.fromJson(json['categoria']) : null, subCategories: json['categorias'] != null ? CategoriaModel.fromJsonArray(json['categorias']) : [], active: json['activo'], ); } // Este metodo sirve para para convertir una instancia de CategoryModel a JSON Map toJson() { return { "id": id, "version": version, "clave": key, "nombre": name, "fechaCreado": createdDate, "categoria": parentCategory?.toJson(), "categorias": subCategories != null ? CategoriaModel.toJsonArray(subCategories!) : [], "activo": active, }; } // Método para crear una lista de CategoryModel desde un JSON static List fromJsonArray(json) { if (json == null) return []; var list = json as List; List listResult = list.map((data) => CategoriaModel.fromJson(data)).toList(); return listResult; } static toJsonArray(List list) { List> listMap = []; for (CategoriaModel item in list) { listMap.add(item.toJson()); } return listMap; } }