RafaelBarbosatec / qr_code_dart_scan

A QR code scanner that works on both iOS and Android using dart decoder.
MIT License
15 stars 20 forks source link

Continuous callbacks even after detecting the QR Code #9

Closed jazz-mobility closed 7 months ago

jazz-mobility commented 11 months ago

First of all thanks for this amazing library. It is very easy to use. :)

I am using the live mode to auto-detect the QR codes and then dismiss the view presented as the bottom modal sheet.

      QRCodeDartScanView(
        scanInvertedQRCode: true,
        typeScan: TypeScan.live,
        formats: [
          BarcodeFormat.QR_CODE,
        ],
        onCapture: (Result result) {
          onCapture(result.text);
          Navigator.of(context).pop();
        },
      ),

I figured that the library kept calling the onCapture with Results even after it detected the QR code, what is the reason for this? You can verify it from the logs below.

flutter: Exception: NotFoundException
flutter: Text: NSABEI
flutter: Text: NSABEI
flutter: Text: NSABEI
flutter: Text: NSABEI
flutter: Text: NSABEI
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No element
#0      ListBase.lastWhere (dart:collection/list.dart:145:5)
#1      NavigatorState.pop (package:flutter/src/widgets/navigator.dart:5043:40)
#2      QRCodeScanner.build.<anonymous closure> (package:qr_code/src/qr_code_scanner.dart:19:33)
#3      QRCodeDartScanViewState._processImage (package:qr_code_dart_scan/src/qr_code_dart_scan_view.dart:142:25)

As I get multiple callbacks in the onCapture, my Navigation stack gets messed up due to the multiple .pop() calls, which is undesirable.

After a quick look into the code, I propose to stop the Image stream after calling onCapture. It will return the scanned value and also will optimize the library performance. Adding await controller?.stopImageStream(); after this line works for me, let me know what you think. https://github.com/RafaelBarbosatec/qr_code_dart_scan/blob/45979ab8ded25f5c24ab6258789271efde84a546/lib/src/qr_code_dart_scan_view.dart#L142

jazz-mobility commented 11 months ago

@RafaelBarbosatec can you please take a look and get this merged if it looks good?

RafaelBarbosatec commented 11 months ago

Hi @jazz-mobility ! Thanks for reporting this issue. So, but if someone not want stop stream when captured the first one?

jazz-mobility commented 11 months ago

Hey @RafaelBarbosatec Sure, there can be such use cases. We can add a property to configure scanning type. I'll update the PR next week. if you have time earlier, feel free to update it. Thanks :)

jazz-mobility commented 11 months ago

I see you fixed it. Nice one. ❤️