class FloodStation { final String id; final String town; final double lat; final double long; final DateTime? dateOpened; final String catchmentName; final String label; final String riverName; FloodStation({ required this.id, required this.town, required this.lat, required this.long, this.dateOpened, required this.catchmentName, required this.label, required this.riverName, }); factory FloodStation.fromMap(Map json) => FloodStation( id: json['wiskiID'] ?? '', town: json['town'] ?? '', lat: parseDoubleValue(json['lat']), long: parseDoubleValue(json['long']), dateOpened: DateTime.tryParse(json['dateOpened'] ?? ''), catchmentName: parseStringValue(json['catchmentName']), label: parseStringValue(json['label']), riverName: parseStringValue(json['riverName']), ); // sometimes the API returns a String instead of a double static double parseDoubleValue(dynamic value) { if (value is double) return value; if (value is String) return double.parse(value); return 0; } // sometimes the API returns a list of labels that are basically identical /// if [value] is a List, the method return the first item static String parseStringValue(dynamic value) { if (value is String) return value; if (value is List) return value[0]; return ''; } }