From 92d2b224e75ad261e4b6e1149c718c32ddac51cf Mon Sep 17 00:00:00 2001 From: Zach Date: Wed, 28 Jan 2026 21:28:28 -0700 Subject: [PATCH] fix: address PR review issues - Fix memory leak by adding dispose() to remove connection listener - Fix typo: changedNavgation -> _changedNavigation - Add mounted check before navigation to prevent errors - Remove overly aggressive _handleDisconnection() call on battery request failure - Only reset battery flag on error to allow retry without disconnecting --- lib/connector/meshcore_connector.dart | 2 +- lib/screens/scanner_screen.dart | 34 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index c378bff..191f74e 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -963,7 +963,7 @@ class MeshCoreConnector extends ChangeNotifier { await sendFrame(buildGetBattAndStorageFrame()); } catch (e) { // Reset flag on error to allow retry - _handleDisconnection(); + // Don't disconnect on battery request failure - it may be transient _batteryRequested = false; } } diff --git a/lib/screens/scanner_screen.dart b/lib/screens/scanner_screen.dart index 642ce1f..0d38d98 100644 --- a/lib/screens/scanner_screen.dart +++ b/lib/screens/scanner_screen.dart @@ -16,25 +16,37 @@ class ScannerScreen extends StatefulWidget { } class _ScannerScreenState extends State { - bool changedNavgation = false; + bool _changedNavigation = false; + late final VoidCallback _connectionListener; @override void initState() { super.initState(); final connector = Provider.of(context, listen: false); - connector.addListener(() { + _connectionListener = () { if (connector.state == MeshCoreConnectionState.disconnected) { - changedNavgation = false; - }else if (connector.state == MeshCoreConnectionState.connected && !changedNavgation) { - changedNavgation = true; - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const ContactsScreen(), - ), - ); + _changedNavigation = false; + } else if (connector.state == MeshCoreConnectionState.connected && !_changedNavigation) { + _changedNavigation = true; + if (mounted) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const ContactsScreen(), + ), + ); + } } - }); + }; + + connector.addListener(_connectionListener); + } + + @override + void dispose() { + final connector = Provider.of(context, listen: false); + connector.removeListener(_connectionListener); + super.dispose(); } @override