85 lines
1.8 KiB
Dart
85 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
|
|
class User {
|
|
final String? id;
|
|
final String nom;
|
|
final String prenom;
|
|
final String email;
|
|
final String password;
|
|
|
|
User({
|
|
this.id,
|
|
required this.nom,
|
|
required this.prenom,
|
|
required this.email,
|
|
required this.password,
|
|
});
|
|
|
|
// Constructeur pour créer un User depuis un Map (utile pour Firebase)
|
|
factory User.fromMap(Map<String, dynamic> map) {
|
|
return User(
|
|
id: map['id'],
|
|
nom: map['nom'] ?? '',
|
|
prenom: map['prenom'] ?? '',
|
|
email: map['email'] ?? '',
|
|
password: map['password'] ?? '',
|
|
);
|
|
}
|
|
|
|
// Constructeur pour créer un User depuis JSON
|
|
factory User.fromJson(String jsonStr) {
|
|
Map<String, dynamic> map = json.decode(jsonStr);
|
|
return User.fromMap(map);
|
|
}
|
|
|
|
// Méthode pour convertir un User en Map (utile pour Firebase)
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'id': id,
|
|
'nom': nom,
|
|
'prenom': prenom,
|
|
'email': email,
|
|
'password': password,
|
|
};
|
|
}
|
|
|
|
// Méthode pour convertir un User en JSON
|
|
String toJson() {
|
|
return json.encode(toMap());
|
|
}
|
|
|
|
// Méthode pour obtenir le nom complet
|
|
String get fullName => '$prenom $nom';
|
|
|
|
// Méthode pour créer une copie avec des modifications
|
|
User copyWith({
|
|
String? id,
|
|
String? nom,
|
|
String? prenom,
|
|
String? email,
|
|
String? password,
|
|
}) {
|
|
return User(
|
|
id: id ?? this.id,
|
|
nom: nom ?? this.nom,
|
|
prenom: prenom ?? this.prenom,
|
|
email: email ?? this.email,
|
|
password: password ?? this.password,
|
|
);
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'User(id: $id, nom: $nom, prenom: $prenom, email: $email)';
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) {
|
|
if (identical(this, other)) return true;
|
|
return other is User && other.email == email;
|
|
}
|
|
|
|
@override
|
|
int get hashCode => email.hashCode;
|
|
}
|