user.model.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'dart:convert';
  2. class User {
  3. final String id;
  4. final String email;
  5. final String createdAt;
  6. final String firstName;
  7. final String lastName;
  8. User({
  9. required this.id,
  10. required this.email,
  11. required this.createdAt,
  12. required this.firstName,
  13. required this.lastName,
  14. });
  15. User copyWith({
  16. String? id,
  17. String? email,
  18. String? createdAt,
  19. String? firstName,
  20. String? lastName,
  21. }) {
  22. return User(
  23. id: id ?? this.id,
  24. email: email ?? this.email,
  25. createdAt: createdAt ?? this.createdAt,
  26. firstName: firstName ?? this.firstName,
  27. lastName: lastName ?? this.lastName,
  28. );
  29. }
  30. Map<String, dynamic> toMap() {
  31. final result = <String, dynamic>{};
  32. result.addAll({'id': id});
  33. result.addAll({'email': email});
  34. result.addAll({'createdAt': createdAt});
  35. return result;
  36. }
  37. factory User.fromMap(Map<String, dynamic> map) {
  38. return User(
  39. id: map['id'] ?? '',
  40. email: map['email'] ?? '',
  41. createdAt: map['createdAt'] ?? '',
  42. firstName: map['firstName'] ?? '',
  43. lastName: map['lastName'] ?? '',
  44. );
  45. }
  46. String toJson() => json.encode(toMap());
  47. factory User.fromJson(String source) => User.fromMap(json.decode(source));
  48. @override
  49. String toString() =>
  50. 'UserInfo(id: $id, email: $email, createdAt: $createdAt)';
  51. @override
  52. bool operator ==(Object other) {
  53. if (identical(this, other)) return true;
  54. return other is User &&
  55. other.id == id &&
  56. other.email == email &&
  57. other.createdAt == createdAt &&
  58. other.firstName == firstName &&
  59. other.lastName == lastName;
  60. }
  61. @override
  62. int get hashCode => id.hashCode ^ email.hashCode ^ createdAt.hashCode;
  63. }