From 48b161cb2c6b3da90fd9ff66237b393e90e6011b Mon Sep 17 00:00:00 2001 From: Zahar Date: Fri, 26 Sep 2025 14:58:49 +0800 Subject: [PATCH] feat(map): improve error handling and widget state checks in map interactions --- lib/screens/map/map_screen.dart | 51 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/screens/map/map_screen.dart b/lib/screens/map/map_screen.dart index e370ad4..fc5a5a4 100644 --- a/lib/screens/map/map_screen.dart +++ b/lib/screens/map/map_screen.dart @@ -82,20 +82,35 @@ class _MapScreenState extends State { } Future _centerOnDeviceOrDefault() async { + // Ensure the widget is still mounted before performing operations + if (!mounted) return; + try { final pos = await LocationService.getBestAvailablePosition(); if (pos != null) { debugPrint( '[map] _centerOnDeviceOrDefault: moving to device location (${pos.latitude}, ${pos.longitude})', ); - _mapController.move(LatLng(pos.latitude, pos.longitude), _defaultZoom); + try { + _mapController.move( + LatLng(pos.latitude, pos.longitude), + _defaultZoom, + ); + } catch (e) { + debugPrint('[map] Error moving to device location: $e'); + } return; } } catch (_) {} + debugPrint( '[map] _centerOnDeviceOrDefault: moving to default center ($_defaultCenter) zoom=$_defaultZoom', ); - _mapController.move(_defaultCenter, _defaultZoom); + try { + _mapController.move(_defaultCenter, _defaultZoom); + } catch (e) { + debugPrint('[map] Error moving to default center: $e'); + } } void _applyFilters() { @@ -131,7 +146,7 @@ class _MapScreenState extends State { } void _fitToBounds() { - if (_filteredReports.isEmpty) return; + if (_filteredReports.isEmpty || !mounted) return; final points = _filteredReports .map((r) => LatLng(r.location.lat, r.location.lng)) .toList(); @@ -141,8 +156,8 @@ class _MapScreenState extends State { _mapController.fitCamera( CameraFit.bounds(bounds: bounds, padding: const EdgeInsets.all(60)), ); - } catch (_) { - // ignore + } catch (e) { + debugPrint('[map] Error fitting to bounds: $e'); } } @@ -276,8 +291,9 @@ class _MapScreenState extends State { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( - I18n.t('map.openedInMyReports') ?? - I18n.t('nav.myReports'), + I18n.t('map.openedInMyReports').isNotEmpty + ? I18n.t('map.openedInMyReports') + : I18n.t('nav.myReports'), ), ), ); @@ -626,6 +642,7 @@ class _MapScreenState extends State { ); }, onClusterTap: (cluster) { + if (!mounted) return; try { final pts = cluster.markers .map((m) => m.point) @@ -637,7 +654,11 @@ class _MapScreenState extends State { padding: const EdgeInsets.all(60), ), ); - } catch (_) {} + } catch (e) { + debugPrint( + '[map] Error fitting cluster bounds: $e', + ); + } }, ), ), @@ -714,20 +735,6 @@ class _MapScreenState extends State { ); } - Widget _legendItem(Severity s, String label) { - return Row( - children: [ - Container( - width: 12, - height: 12, - decoration: BoxDecoration(color: s.color, shape: BoxShape.circle), - ), - const SizedBox(width: 6), - Text(label, style: const TextStyle(fontSize: 12)), - ], - ); - } - Widget _enhancedLegendItem(Severity s, String label) { return Row( children: [