|
| 1 | +class WateringHistoryModel { |
| 2 | + final int? id; |
| 3 | + final int plantId; |
| 4 | + final DateTime wateredAt; |
| 5 | + |
| 6 | + WateringHistoryModel({ |
| 7 | + this.id, |
| 8 | + required this.plantId, |
| 9 | + required this.wateredAt, |
| 10 | + }); |
| 11 | + |
| 12 | + /// Creates a WateringHistoryModel from a JSON object. |
| 13 | + factory WateringHistoryModel.fromJson(Map<String, dynamic> json) { |
| 14 | + return WateringHistoryModel( |
| 15 | + id: json['id'], |
| 16 | + plantId: json['plant_id'], |
| 17 | + wateredAt: |
| 18 | + json['watered_at'] is String |
| 19 | + ? DateTime.parse(json['watered_at']) |
| 20 | + : json['watered_at'], |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + Map<String, dynamic> toJson() { |
| 25 | + return { |
| 26 | + 'id': id, |
| 27 | + 'plant_id': plantId, |
| 28 | + 'watered_at': wateredAt.toIso8601String(), |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + WateringHistoryModel copyWith({int? id, int? plantId, DateTime? wateredAt}) { |
| 33 | + return WateringHistoryModel( |
| 34 | + id: id ?? this.id, |
| 35 | + plantId: plantId ?? this.plantId, |
| 36 | + wateredAt: wateredAt ?? this.wateredAt, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + bool operator ==(Object other) { |
| 42 | + if (identical(this, other)) return true; |
| 43 | + |
| 44 | + return other is WateringHistoryModel && |
| 45 | + other.id == id && |
| 46 | + other.plantId == plantId && |
| 47 | + other.wateredAt == wateredAt; |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + int get hashCode => id.hashCode ^ plantId.hashCode ^ wateredAt.hashCode; |
| 52 | + |
| 53 | + @override |
| 54 | + String toString() => |
| 55 | + 'WateringHistoryModel(id: $id, plantId: $plantId, wateredAt: $wateredAt)'; |
| 56 | +} |
0 commit comments