74 lines
2.4 KiB
Dart
74 lines
2.4 KiB
Dart
// Importa le classi annidate
|
|
import 'contact.dart';
|
|
import 'vineyard.dart';
|
|
import 'address.dart';
|
|
|
|
class Owner {
|
|
final int id;
|
|
final String? name;
|
|
final String? surname;
|
|
final String? companyName;
|
|
final bool isActive;
|
|
final DateTime creationDate;
|
|
final String note;
|
|
final List<Contact>? contacts;
|
|
final List<Vineyard>? vineyards;
|
|
final List<Address>? addresses;
|
|
|
|
// Costruttore con parametri nominati e 'required'
|
|
Owner({
|
|
required this.id,
|
|
this.name,
|
|
this.surname,
|
|
this.companyName,
|
|
required this.isActive,
|
|
required this.creationDate,
|
|
required this.note,
|
|
this.contacts,
|
|
this.vineyards,
|
|
this.addresses,
|
|
});
|
|
|
|
// ---
|
|
|
|
/// Converte l'oggetto Owner in una mappa per la serializzazione JSON.
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'name': name,
|
|
'surname': surname,
|
|
'companyName': companyName,
|
|
'isActive': isActive,
|
|
'creationDate': creationDate.toIso8601String(),
|
|
'note': note,
|
|
'contacts': contacts?.map((contact) => contact.toJson()).toList(),
|
|
'vineyards': vineyards?.map((vineyard) => vineyard.toJson()).toList(),
|
|
'addresses': addresses?.map((address) => address.toJson()).toList(),
|
|
};
|
|
}
|
|
|
|
/// Crea un oggetto Owner da una mappa (es. da una risposta JSON).
|
|
factory Owner.fromJson(Map<String, dynamic> json) {
|
|
try{
|
|
return Owner(
|
|
id: json['id'] as int,
|
|
name: json['name'] as String,
|
|
surname: json['surname'] as String,
|
|
companyName: json['companyName'] as String,
|
|
isActive: json['isActive'] as bool,
|
|
creationDate: DateTime.parse(json['creationDate'] as String),
|
|
note: json['note'] as String,
|
|
// Mappa ogni elemento JSON in un oggetto Contact
|
|
contacts: (json['contacts'] as List).map((i) => Contact.fromJson(i as Map<String, dynamic>)).toList(),
|
|
// Mappa ogni elemento JSON in un oggetto Vineyard
|
|
vineyards: (json['vineyards'] as List).map((i) => Vineyard.fromJson(i as Map<String, dynamic>)).toList(),
|
|
// Mappa ogni elemento JSON in un oggetto Address
|
|
addresses: (json['addresses'] as List).map((i) => Address.fromJson(i as Map<String, dynamic>)).toList(),
|
|
);
|
|
}
|
|
catch (e) {
|
|
print('Errore durante la deserializzazione dell\'oggetto Owner: $e');
|
|
return Owner(id: 0, isActive: false, creationDate: DateTime.now(), note: ''); // Restituisce un oggetto Owner vuoto in caso di errore
|
|
}
|
|
}
|
|
} |