init
This commit is contained in:
commit
e833bcaec4
224 files changed
+11812
No files matched your search
@@ -0,0 +1,61 @@
|
||||
// Importa le classi annidate
|
||||
import 'role.dart';
|
||||
import 'owner.dart';
|
||||
|
||||
class User {
|
||||
final int? id;
|
||||
final String? username;
|
||||
final String? password;
|
||||
final Role? role;
|
||||
final bool? active;
|
||||
final List<Owner>? owners;
|
||||
|
||||
// Costruttore con parametri nominati e 'required'
|
||||
User({
|
||||
this.id,
|
||||
this.username,
|
||||
this.password,
|
||||
this.role,
|
||||
this.active,
|
||||
this.owners,
|
||||
});
|
||||
|
||||
// ---
|
||||
|
||||
/// Converte l'oggetto User in una mappa per la serializzazione JSON.
|
||||
Map<String, dynamic> toJson() {
|
||||
try{
|
||||
return {
|
||||
'id': id,
|
||||
'username': username,
|
||||
'password': password,
|
||||
'role': role?.toJson(), // Chiama il metodo toJson() della classe Role
|
||||
'active': active,
|
||||
'owners': owners?.map((owner) => owner.toJson()).toList(), // Converte la lista in una lista di mappe JSON
|
||||
};
|
||||
} catch (e) {
|
||||
print('Errore durante la serializzazione dell\'oggetto User: $e');
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un oggetto User da una mappa (es. da una risposta JSON).
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
final Map<String, dynamic> userJson = json['user'] as Map<String, dynamic>;
|
||||
try{
|
||||
return User(
|
||||
id: userJson['id'] as int,
|
||||
username: userJson['username'] as String?,
|
||||
password: userJson['password'] as String?,
|
||||
role: Role.fromJson(userJson['role'] as Map<String, dynamic>), // Crea un oggetto Role dalla mappa annidata
|
||||
active: json['active'] as bool?,
|
||||
owners: (userJson['owners'] as List)
|
||||
.map((i) => Owner.fromJson(i as Map<String, dynamic>))
|
||||
.toList(), // Mappa ogni elemento JSON in un oggetto Owner
|
||||
);
|
||||
} catch (e) {
|
||||
print('Errore durante la deserializzazione dell\'oggetto User: $e');
|
||||
return User(); // Restituisce un oggetto User vuoto in caso di errore
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user