import 'dart:convert'; class TankModel { final String szDocumentId; final String szUserId; final String szDate; final String szOdometer; final String szLiters; final String szPricePerLiter; final String szLocation; final String szPriceTotal; TankModel({ required this.szDocumentId, required this.szUserId, required this.szDate, required this.szOdometer, required this.szLiters, required this.szPricePerLiter, required this.szLocation, required this.szPriceTotal, }); TankModel copyWith({ String? szDocumentId, String? szUserId, String? szDate, String? szOdometer, String? szLiters, String? szPricePerLiter, String? szLocation, String? szPriceTotal, }) { return TankModel( szDocumentId: szDocumentId ?? this.szDocumentId, szUserId: szUserId ?? this.szUserId, szDate: szDate ?? this.szDate, szOdometer: szOdometer ?? this.szOdometer, szLiters: szLiters ?? this.szLiters, szPricePerLiter: szPricePerLiter ?? this.szPricePerLiter, szLocation: szLocation ?? this.szLocation, szPriceTotal: szPriceTotal ?? this.szPriceTotal, ); } Map toMap() { final result = {}; result.addAll({'\$id': szDocumentId}); result.addAll({'userId': szUserId}); result.addAll({'date': szDate}); result.addAll({'odometer': szOdometer}); result.addAll({'liters': szLiters}); result.addAll({'pricePerLiter': szPricePerLiter}); result.addAll({'location': szLocation}); result.addAll({'priceTotal': szPriceTotal}); return result; } factory TankModel.fromMap(Map map) { return TankModel( szDocumentId: map['\$id'] ?? '', szUserId: map['userId'] ?? '', szDate: map['date'] ?? '', szOdometer: map['odometer'] ?? '', szLiters: map['liters'] ?? '', szPricePerLiter: map['pricePerLiter'] ?? '', szLocation: map['location'] ?? '', szPriceTotal: (double.parse(map['liters']?.toString() ?? '0') * double.parse(map['pricePerLiter']?.toString() ?? '0')).toStringAsFixed(2), ); } String toJson() => json.encode(toMap()); factory TankModel.fromJson(String source) => TankModel.fromMap(json.decode(source)); @override String toString() { return 'TankModel(szDocumentId: $szDocumentId, szUserId: $szUserId, szDate: $szDate, szOdometer: $szOdometer, szLiters: $szLiters, szPricePerLiter: $szPricePerLiter, szLocation: $szLocation, szPriceTotal: $szPriceTotal)'; } }