mobile_scanner icon indicating copy to clipboard operation
mobile_scanner copied to clipboard

[Bug] DetectionSpeed.noDuplicates is not respected properly

Open burekas7 opened this issue 1 year ago • 14 comments

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?

burekas7 avatar May 27 '24 14:05 burekas7

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.

CarlosVallsCiscar avatar Jun 04 '24 09:06 CarlosVallsCiscar

Thanks for the observations. I'll have to take a look at why the listener is called twice specifically.

navaronbracke avatar Jun 04 '24 09:06 navaronbracke

Currently having the same issue, the listener is getting called twice when scanning a barcode

huideaki avatar Jun 04 '24 19:06 huideaki

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 avatar Jun 05 '24 12:06 aimeykra

@

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 avatar Jun 07 '24 08:06 tajjacob

@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 avatar Jun 09 '24 02:06 aimeykra

@aimeykra thanks for the snippet! its working!

tajjacob avatar Jun 10 '24 03:06 tajjacob

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.

navaronbracke avatar Jun 11 '24 13:06 navaronbracke

I have the same issue.

ahua666 avatar Jun 25 '24 06:06 ahua666

Temporarily solved it through business code.

ahua666 avatar Jun 25 '24 07:06 ahua666

@navaronbracke Is there any progress with this issue?

Thanks.

burekas7 avatar Sep 11 '24 08:09 burekas7

I have not looked into this yet. Mostly busy with other things at the moment.

navaronbracke avatar Sep 11 '24 09:09 navaronbracke

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.

Dohmanlechx avatar Sep 25 '24 09:09 Dohmanlechx

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....

}

GRHgabriel avatar Sep 30 '24 08:09 GRHgabriel

Adding autoStart: false to the controller solved it for me:

final MobileScannerController controller = MobileScannerController(autoStart: false,
      detectionSpeed: DetectionSpeed.noDuplicates);

YahiaAngelo avatar Nov 07 '24 10:11 YahiaAngelo

Adding autoStart: false to 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.

atishoo avatar Dec 03 '24 16:12 atishoo

Does it require to upgrade to a new version of the package? I am using v5.1.1.

aimeykra avatar Dec 04 '24 02:12 aimeykra

Any progress about this issue?

ghost avatar Feb 24 '25 02:02 ghost

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.

juliansteenbakker avatar Apr 25 '25 07:04 juliansteenbakker