// Assumiamo che la classe VinePlant sia stata definita in un file 'vine_plant.dart' import 'vine.plant.dart'; class Row { final int id; final int vineyardId; final int rowNum; final double length; final List? vinePlants; // Costruttore con parametri nominati. // 'vinePlants' è opzionale perché è nullable. Row({ required this.id, required this.vineyardId, required this.rowNum, required this.length, this.vinePlants, }); // --- /// Converte l'oggetto Row in una mappa per la serializzazione JSON. Map toJson() { return { 'id': id, 'vineyardId': vineyardId, 'rowNum': rowNum, 'length': length, // Se la lista non è null, la converte in una lista di mappe 'vinePlants': vinePlants?.map((plant) => plant.toJson()).toList(), }; } /// Crea un oggetto Row da una mappa (es. da una risposta JSON). factory Row.fromJson(Map json) { // La lista viene creata solo se esiste e non è null nella mappa JSON final List? plantsJson = json['vinePlants']; final List? plantsList = plantsJson != null ? plantsJson.map((json) => VinePlant.fromJson(json as Map)).toList() : null; return Row( id: json['id'] as int, vineyardId: json['vineyardId'] as int, rowNum: json['rowNum'] as int, length: (json['length'] as num).toDouble(), vinePlants: plantsList, ); } }