mobile_scanner
mobile_scanner copied to clipboard
Scan Only 1 Barcode
I Am Trying to Scan Only 1 Barcode at at time like other packages like flutter_barcode_scanner. Scan 1 barcode process the Result and than scan again. Navigator.pop(context); is behaving abnormally in the onDetect Function. Its gets back 2-3 times or even goes to the start of the app. Tweaking some code below caused debuglocked error
Kindly Help me about how to achieve the above ?
onDetect: (barcode, args) { if (barcode.rawValue == null) { debugPrint('Failed to scan Barcode'); } else { final String code = barcode.rawValue!; debugPrint('Barcode found! $code');
Future.delayed(const Duration(seconds: 2), () {
Navigator.pop(context);
});
}
},
Can Any 1 Help ?
Hi @parmarjitesh I'm assuming u have allowDuplicates
set to true.
I also use it that way since I might want to scan a QR code twice.
To prevent the scanner from getting the same value over and over you could add a debounce to the onDetect
function.
In my case I have something like this:
MobileScanner(
allowDuplicates: true,
controller: controller,
onDetect: (barcode, args) => isEnabled.value ? _onDetect(/*My args here*/) : null,
...
_onDetect(/*My args here*/){
isEnabled.value = false;
// ... my code to run
timer = Timer(
const Duration(seconds: 3),
() => isEnabled.value = true,
);
}
Hope it helps :)