From 8dc4939e2eb055e4ba1463f931c9c69284687973 Mon Sep 17 00:00:00 2001 From: uvok Date: Tue, 29 Jul 2025 14:58:50 +0200 Subject: Fix various bugs/selection clear/all scan results get previous scan resuls as well. --- lib/device_scan_select.dart | 21 +++++++++++++++++++++ lib/main.dart | 10 ++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/device_scan_select.dart b/lib/device_scan_select.dart index 27b7492..7dafe2b 100644 --- a/lib/device_scan_select.dart +++ b/lib/device_scan_select.dart @@ -61,6 +61,27 @@ class _DeviceScanSelectionState extends State { ), ); } + + @override + void didUpdateWidget(covariant DeviceScanSelection oldWidget) { + super.didUpdateWidget(oldWidget); + + if (!_deviceListEqual(oldWidget.items, widget.items)) { + setState(() { + selectedResult = -1; + }); + } + } + + bool _deviceListEqual(List oldList, List newList) { + if (oldList.length != newList.length) return false; + for (int i = 0; i < oldList.length; i++) { + if (oldList[i].device.remoteId != newList[i].device.remoteId) { + return false; + } + } + return true; + } } String firstGiven(List list) { diff --git a/lib/main.dart b/lib/main.dart index a04247b..6310660 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -64,7 +64,7 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - final List scanResults = []; + List scanResults = []; bool isScanning = false; ScanResult? selectedDevice; @@ -91,14 +91,16 @@ class _MyHomePageState extends State { } setState(() { - scanResults.clear(); + selectedDevice = null; + scanResults = []; isScanning = true; - setState(() {}); }); - var subscription = FlutterBluePlus.onScanResults.listen( + var subscription = FlutterBluePlus.scanResults.listen( onScanResult, onError: (e) => logger.e(e), ); + // should probably use this! + FlutterBluePlus.cancelWhenScanComplete(subscription); // Ehhhh... can't have both try { -- cgit v1.2.3