toJson method
Las opciones en la forma que espera el servicio para travelMode.
Se anidan bajo la clave del modo (Truck, Car, Scooter), que es como
las quiere la API. Si el modo no coincide con el del constructor, se
devuelve un mapa vacío en vez de enviar opciones de camión bajo Car,
que provocaría un 400.
Implementation
Map<String, dynamic> toJson(TravelMode travelMode) {
if (travelMode != _kind) return const <String, dynamic>{};
final options = <String, dynamic>{
if (grossWeightKg != null) 'GrossWeight': grossWeightKg,
if (heightCm != null) 'Height': heightCm,
if (lengthCm != null) 'Length': lengthCm,
if (widthCm != null) 'Width': widthCm,
if (axleCount != null) 'AxleCount': axleCount,
if (trailerCount != null) 'TrailerCount': trailerCount,
if (hazardousCargos.isNotEmpty) 'HazardousCargos': hazardousCargos,
if (tunnelRestrictionCode != null)
'TunnelRestrictionCode': tunnelRestrictionCode,
if (truckType != null) 'TruckType': truckType,
if (engineType != null) 'EngineType': engineType,
if (occupancy != null) 'Occupancy': occupancy,
if (licensePlateLastCharacter != null)
'LicensePlate': <String, dynamic>{
'LastCharacter': licensePlateLastCharacter,
},
};
if (options.isEmpty) return const <String, dynamic>{};
return <String, dynamic>{_kind.wireName: options};
}