// lib/models/vehicle.dart import 'dart:convert'; import '../config/enum/vehicle_type.dart'; import 'FileModel.dart'; class Vehicle { final FileModel? carLicence; final int id; final FileModel? insurance; final List images; final String plateNumber; final int timestamp; final String vehicleModel; final VehicleType vehicleType; final int vehicleYear; /// Constructs a [Vehicle] instance. Vehicle({ this.carLicence, required this.id, this.insurance, required this.images, required this.plateNumber, required this.timestamp, required this.vehicleModel, required this.vehicleType, required this.vehicleYear, }); /// Creates a new [Vehicle] instance from a JSON map safely. factory Vehicle.fromJson(Map json) { // ✨ FIX: Safely parse potentially null map for carlicence final carLicenceJson = json['carlicence'] as Map?; // ✨ FIX: Safely parse potentially null map for insurance final insuranceJson = json['insurance'] as Map?; return Vehicle( // Use the parsed JSON if not null, otherwise set property to null carLicence: carLicenceJson != null ? FileModel.fromJson(carLicenceJson) : null, insurance: insuranceJson != null ? FileModel.fromJson(insuranceJson) : null, // This was already safe, no changes needed. images: (json['images'] as List?) ?.map((item) => FileModel.fromJson(item as Map)) .toList() ?? [], // ✨ FIX: Provide default values for primitives using '??' id: json['id'] as int? ?? 0, plateNumber: json['platenumber'] as String? ?? '', timestamp: json['timestmp'] as int? ?? 0, vehicleModel: json['vehiclemodel'] as String? ?? '', vehicleYear: json['vehicleyear'] as int? ?? 0, // ✨ FIX: Safely handle vehicle type parsing vehicleType: _vehicleTypeFromString(json['vehicletype'] as String?), ); } /// Helper method to convert a string to [VehicleType] enum safely. static VehicleType _vehicleTypeFromString(String? type) { // If the type string is null or empty, return a default value if (type == null || type.isEmpty) { return VehicleType.UNKNOWN; // Default value } return VehicleType.values.firstWhere( (e) => e.toString().split('.').last.toUpperCase() == type.toUpperCase(), // If no match is found in the enum, return a default value orElse: () => VehicleType.UNKNOWN, ); } /// Converts the [Vehicle] instance to a JSON map. Map toJson() { return { 'carlicence': carLicence?.toJson(), 'id': id, 'insurance': insurance?.toJson(), 'images': images.map((img) => img.toJson()).toList(), 'platenumber': plateNumber, 'timestmp': timestamp, 'vehiclemodel': vehicleModel, 'vehicletype': _vehicleTypeToString(vehicleType), 'vehicleyear': vehicleYear, }; } /// Creates a [Vehicle] instance from a JSON string. factory Vehicle.fromJsonString(String jsonString) { return Vehicle.fromJson(json.decode(jsonString)); } String toJsonString() { return json.encode(toJson()); } static String _vehicleTypeToString(VehicleType type) { return type.toString().split('.').last; } @override String toString() { return 'Vehicle{carLicence: $carLicence, id: $id, insurance: $insurance, images: $images, plateNumber: $plateNumber, timestamp: $timestamp, vehicleModel: $vehicleModel, vehicleType: $vehicleType, vehicleYear: $vehicleYear}'; } @override bool operator ==(Object other) => identical(this, other) || other is Vehicle && runtimeType == other.runtimeType && carLicence == other.carLicence && id == other.id && insurance == other.insurance && plateNumber == other.plateNumber && timestamp == other.timestamp && vehicleModel == other.vehicleModel && vehicleType == other.vehicleType && vehicleYear == other.vehicleYear; @override int get hashCode => carLicence.hashCode ^ id.hashCode ^ insurance.hashCode ^ plateNumber.hashCode ^ timestamp.hashCode ^ vehicleModel.hashCode ^ vehicleType.hashCode ^ vehicleYear.hashCode; }