这是CaiYouHui前端,一个关于flutter的安卓app,前端使用flutter实现

user.dart 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. class User {
  2. final int id; // 改为int,匹配API
  3. final String username; // 改为username
  4. final String email;
  5. final String? fullName; // 改为fullName
  6. final String? avatar;
  7. final String? phone;
  8. final bool isActive;
  9. final bool isVerified;
  10. final DateTime createdAt;
  11. final DateTime? lastLogin;
  12. User({
  13. required this.id,
  14. required this.username,
  15. required this.email,
  16. this.fullName,
  17. this.avatar,
  18. this.phone,
  19. this.isActive = true,
  20. this.isVerified = false,
  21. required this.createdAt,
  22. this.lastLogin,
  23. });
  24. factory User.fromJson(Map<String, dynamic> json) {
  25. return User(
  26. id: json['id'] ?? 0,
  27. username: json['username'] ?? '',
  28. email: json['email'] ?? '',
  29. fullName: json['full_name'],
  30. avatar: json['avatar'],
  31. phone: json['phone'],
  32. isActive: json['is_active'] ?? true,
  33. isVerified: json['is_verified'] ?? false,
  34. createdAt: json['created_at'] != null
  35. ? DateTime.parse(json['created_at'])
  36. : DateTime.now(),
  37. lastLogin: json['last_login'] != null
  38. ? DateTime.parse(json['last_login'])
  39. : null,
  40. );
  41. }
  42. Map<String, dynamic> toJson() {
  43. return {
  44. 'id': id,
  45. 'username': username,
  46. 'email': email,
  47. 'full_name': fullName,
  48. 'avatar': avatar,
  49. 'is_active': isActive,
  50. 'is_verified': isVerified,
  51. 'created_at': createdAt.toIso8601String(),
  52. 'last_login': lastLogin?.toIso8601String(),
  53. };
  54. }
  55. // 添加一个getter来兼容之前的name字段
  56. // String get name => fullName ?? username;
  57. User copyWith({
  58. int? id,
  59. String? username,
  60. String? email,
  61. String? fullName,
  62. String? avatar,
  63. bool? isActive,
  64. bool? isVerified,
  65. DateTime? createdAt,
  66. DateTime? lastLogin,
  67. }) {
  68. return User(
  69. id: id ?? this.id,
  70. username: username ?? this.username,
  71. email: email ?? this.email,
  72. fullName: fullName ?? this.fullName,
  73. avatar: avatar ?? this.avatar,
  74. isActive: isActive ?? this.isActive,
  75. isVerified: isVerified ?? this.isVerified,
  76. createdAt: createdAt ?? this.createdAt,
  77. lastLogin: lastLogin ?? this.lastLogin,
  78. );
  79. }
  80. }