| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- class User {
- final int id; // 改为int,匹配API
- final String username; // 改为username
- final String email;
- final String? fullName; // 改为fullName
- final String? avatar;
- final String? phone;
- final bool isActive;
- final bool isVerified;
- final DateTime createdAt;
- final DateTime? lastLogin;
-
- User({
- required this.id,
- required this.username,
- required this.email,
- this.fullName,
- this.avatar,
- this.phone,
- this.isActive = true,
- this.isVerified = false,
- required this.createdAt,
- this.lastLogin,
- });
-
- factory User.fromJson(Map<String, dynamic> json) {
- return User(
- id: json['id'] ?? 0,
- username: json['username'] ?? '',
- email: json['email'] ?? '',
- fullName: json['full_name'],
- avatar: json['avatar'],
- phone: json['phone'],
- isActive: json['is_active'] ?? true,
- isVerified: json['is_verified'] ?? false,
- createdAt: json['created_at'] != null
- ? DateTime.parse(json['created_at'])
- : DateTime.now(),
- lastLogin: json['last_login'] != null
- ? DateTime.parse(json['last_login'])
- : null,
- );
- }
-
- Map<String, dynamic> toJson() {
- return {
- 'id': id,
- 'username': username,
- 'email': email,
- 'full_name': fullName,
- 'avatar': avatar,
- 'is_active': isActive,
- 'is_verified': isVerified,
- 'created_at': createdAt.toIso8601String(),
- 'last_login': lastLogin?.toIso8601String(),
- };
- }
-
- // 添加一个getter来兼容之前的name字段
- // String get name => fullName ?? username;
-
- User copyWith({
- int? id,
- String? username,
- String? email,
- String? fullName,
- String? avatar,
- bool? isActive,
- bool? isVerified,
- DateTime? createdAt,
- DateTime? lastLogin,
- }) {
- return User(
- id: id ?? this.id,
- username: username ?? this.username,
- email: email ?? this.email,
- fullName: fullName ?? this.fullName,
- avatar: avatar ?? this.avatar,
- isActive: isActive ?? this.isActive,
- isVerified: isVerified ?? this.isVerified,
- createdAt: createdAt ?? this.createdAt,
- lastLogin: lastLogin ?? this.lastLogin,
- );
- }
- }
|