diff options
author | uvok | 2025-07-29 14:58:50 +0200 |
---|---|---|
committer | uvok | 2025-07-29 14:58:50 +0200 |
commit | 8dc4939e2eb055e4ba1463f931c9c69284687973 (patch) | |
tree | c042c5f463c27809da039fde479a3bdb15746dbc | |
parent | 7c0f47a946939875b13250b204898911fc525827 (diff) |
Fix various bugs/selection clear/all scan results
get previous scan resuls as well.
-rw-r--r-- | lib/device_scan_select.dart | 21 | ||||
-rw-r--r-- | lib/main.dart | 10 |
2 files changed, 27 insertions, 4 deletions
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<DeviceScanSelection> { ), ); } + + @override + void didUpdateWidget(covariant DeviceScanSelection oldWidget) { + super.didUpdateWidget(oldWidget); + + if (!_deviceListEqual(oldWidget.items, widget.items)) { + setState(() { + selectedResult = -1; + }); + } + } + + bool _deviceListEqual(List<ScanResult> oldList, List<ScanResult> 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<String> 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<MyHomePage> { - final List<ScanResult> scanResults = []; + List<ScanResult> scanResults = []; bool isScanning = false; ScanResult? selectedDevice; @@ -91,14 +91,16 @@ class _MyHomePageState extends State<MyHomePage> { } 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 { |