mobile_scanner
mobile_scanner copied to clipboard
[Bug] DetectionSpeed.noDuplicates is not respected properly
Hi,
@navaronbracke
I'm using the controller in order to catch scans, And I'm using the "barcodes" stream like that:
mobileScannerController!.barcodes.listen(scannerlistener);
But it seems that on each scan, 'scannerlistener' is running twice. Is it a bug?
I have the same problem. I use the same as you to listen to the barcodes, and I always get two calls to the handler.
mobile_scanner: ^5.1.1 Flutter 3.22.1
I have tested the problem on iOS, but the user who detected the problem was on android.
This is the logger, i'm only listening to the events.
flutter: INFO 2024-06-04 11:06:42.063180 QrScanner: Instance of 'BarcodeCapture'
flutter: INFO 2024-06-04 11:06:42.063457 QrScanner: Instance of 'BarcodeCapture'
I checked the result of the 'BarcodeCapture' and is only 1 barcode in barcodes list and the rawBytes list is the same in both.
The controller configuration
final MobileScannerController _controller = MobileScannerController(
detectionSpeed: DetectionSpeed.noDuplicates,
facing: CameraFacing.back,
torchEnabled: false,
formats: [BarcodeFormat.qrCode]
);
For now, as I have the QR check in the main thread, I have used a lock to avoid problems.
Thanks for the observations. I'll have to take a look at why the listener is called twice specifically.
Currently having the same issue, the listener is getting called twice when scanning a barcode
Thank you guys for posting this. I am facing same issue: Scaffold 'A' opens scaffold 'B'. Scaffold 'B' opens Scaffold 'C' which has MobileScanner widget. in my OnDetect or Listener call back, I have a Navigator.pop(). When a QR code is scanned, it triggers 2 'pop' and I am being sent back to scaffold 'A' instead of Scaffold 'B'.
I noticed that the double call back was not happening all the time, per my test, I would say 7 or 8 out of 10.
I added a workaround in my code to ignore the second callback and it's all working fine now. my scenario is simple though, i only needed one single scan, may not be the case for others.
Thanks. AK
@
Thank you guys for posting this. I am facing same issue: Scaffold 'A' opens scaffold 'B'. Scaffold 'B' opens Scaffold 'C' which has MobileScanner widget. in my OnDetect or Listener call back, I have a Navigator.pop(). When a QR code is scanned, it triggers 2 'pop' and I am being sent back to scaffold 'A' instead of Scaffold 'B'.
I noticed that the double call back was not happening all the time, per my test, I would say 7 or 8 out of 10.
I added a workaround in my code to ignore the second callback and it's all working fine now. my scenario is simple though, i only needed one single scan, may not be the case for others.
Thanks. AK
@aimeykra can you share the code snippet? I'm also having the same issue
@tajjacob , I am not doing anything fancy, I am just saving a status in a boolean variable to indicate that i have already sent the scanned data.
here is my OnDetect:
onDetect: (capture) { final List<Barcode> barcodes = capture.barcodes; for (final barcode in barcodes) { if (!_isDataSent) { Navigator.pop<String>(context, barcode.rawValue ?? 'No data in QR'); _isDataSent = true; } }
Again, this is fine for my scenario. i have a single QR to scan at a time.
hope it helps. @aimeykra
@aimeykra thanks for the snippet! its working!
I'm currently going to look into this, as this is now also affecting an internal app.
Edit: It seems that the noDuplicates mode is not working correctly. Even in the example app, using it does not prevent the same barcode from being passed to the detection callback.
I have the same issue.
Temporarily solved it through business code.
@navaronbracke Is there any progress with this issue?
Thanks.
I have not looked into this yet. Mostly busy with other things at the moment.
Following this issue as I'm having the same problem. I don't intend to use DetectionSpeed.noDuplicates but I still get duplicated results (2 of each) despite I use detectionTimeoutMs: 2000.
I handle this issue temporarily like this:
String? lastScan;
void _handleBarcode(BarcodeCapture barcodes) async { String? data = barcodes.barcodes.first.rawValue;
if (lastScan == data) {
return;
}
lastScan = data;
//your process code here....
}
Adding autoStart: false to the controller solved it for me:
final MobileScannerController controller = MobileScannerController(autoStart: false,
detectionSpeed: DetectionSpeed.noDuplicates);
Adding
autoStart: falseto the controller solved it for me:final MobileScannerController controller = MobileScannerController(autoStart: false, detectionSpeed: DetectionSpeed.noDuplicates);
yes, you are right bro! this way works for me.
Does it require to upgrade to a new version of the package? I am using v5.1.1.
Any progress about this issue?
I'm closing this as there is no activity, and newer versions of this plug are already released. If this still happens with the latest version let me know by commenting.