From 706b78b737fbd4fc62c7ca8ede4c7fb4327f7e4c Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Mon, 6 Jul 2026 22:00:29 -0500 Subject: [PATCH] fix: safe parsing of Prisma Decimal fields to prevent data loss on save Prisma serializes Decimal fields (rate, latitude, longitude) as JSON strings, causing 'as num?' casts to throw in fromDocument(). The catch block in getProfessional() then returned an empty Profesional, so saving overwrote all existing data with empty strings. Also added try-catch in all patch API calls to surface errors to user instead of silently failing with a stuck loading button. Co-Authored-By: Claude Sonnet 4.6 --- lib/models/profesional.dart | 10 +++++---- lib/providers/professional_form_provider.dart | 22 +++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/models/profesional.dart b/lib/models/profesional.dart index b4f4b73..4838b9f 100644 --- a/lib/models/profesional.dart +++ b/lib/models/profesional.dart @@ -134,14 +134,16 @@ class Profesional { address: (doc['address'] as String?) ?? '', aditionalAddress: (doc['additional_address'] as String?) ?? '', profession: (doc['profession'] as String?) ?? '', - ratePreferences: (doc['rate_preferences'] as bool?) ?? false, - rate: (doc['rate'] as num?)?.toString() ?? '', + ratePreferences: doc['rate_preferences'] is bool + ? doc['rate_preferences'] as bool + : doc['rate_preferences'] == 'true', + rate: doc['rate']?.toString() ?? '', locationPreferences: locationPrefsFromValue(doc['location_preferences']), bannerPicture: (doc['banner_picture'] as String?) ?? '', identificationPicture: (doc['identification_picture'] as String?) ?? '', certificatePicture: (doc['certificate_picture'] as String?) ?? '', - latitude: (doc['latitude'] as num?)?.toDouble() ?? 0.0, - longitude: (doc['longitude'] as num?)?.toDouble() ?? 0.0, + latitude: double.tryParse(doc['latitude']?.toString() ?? '') ?? 0.0, + longitude: double.tryParse(doc['longitude']?.toString() ?? '') ?? 0.0, specializations: specs, specializationsPictures: specPics, schedules: parsedSchedules, diff --git a/lib/providers/professional_form_provider.dart b/lib/providers/professional_form_provider.dart index 5e16db0..b422268 100644 --- a/lib/providers/professional_form_provider.dart +++ b/lib/providers/professional_form_provider.dart @@ -72,7 +72,12 @@ class ProfessionalFormProvider with ChangeNotifier { Future updateProfesionalInfo(String userId) async { if (!_validForm()) return false; - await _api.patch('/professionals/me', profesional!.toDocument()); + try { + await _api.patch('/professionals/me', profesional!.toDocument()); + } catch (e) { + NotificationsService.showSnackbar('Error al guardar: $e'); + return false; + } NotificationsService.showSnackbar('Información actualizada'); return true; } @@ -84,8 +89,12 @@ class ProfessionalFormProvider with ChangeNotifier { Future updateProfesionalProfileInfo(String userId) async { if (!_validProfileForm()) return false; - await _api.patch('/professionals/me', profesional!.toDocument()); - // Re-fetch from server to confirm saved data is reflected in the form + try { + await _api.patch('/professionals/me', profesional!.toDocument()); + } catch (e) { + NotificationsService.showSnackbar('Error al guardar: $e'); + return false; + } try { final data = await _api.get('/professionals/me'); profesional = Profesional.fromDocument(data as Map); @@ -96,7 +105,12 @@ class ProfessionalFormProvider with ChangeNotifier { } Future updateProfesionalProfileScheduleInfo(String userId) async { - await _api.patch('/professionals/me', profesional!.toDocument()); + try { + await _api.patch('/professionals/me', profesional!.toDocument()); + } catch (e) { + NotificationsService.showSnackbar('Error al guardar: $e'); + return false; + } NotificationsService.showSnackbar('Información actualizada'); return true; }