set interval for QR coder scanner in flutter

483

This code will check if the last scan was performed less than the specified duration, if the duration exceeds it will print the data

DateTime? lastScan;
...
_qRViewController?.scannedDataStream.listen((data) {
  final currentScan = DateTime.now();
  if (lastScan == null || currentScan.difference(lastScan!) > const Duration(seconds: 1)) {
    lastScan = currentScan;
    print(data.code);
  }
});
Share:
483
Vee
Author by

Vee

Updated on December 06, 2022

Comments

  • Vee
    Vee over 1 year

    I need to scan the same barcode for many times . I'm using qr_code_scanner plugin for scanning. problem is it scans continuously without any gap .so when i scanned two times , it takes as 5 or 6 times like that. I need to set interval . Any help might help . Thanks .

    '''updateQRView(QRViewController controller) {
        qrController = controller;
    
        qrController?.scannedDataStream?.listen((data) {
          validateBulkReceive(data.code.toString());
          Timer timer = new Timer(new Duration(seconds: 1), () {
          });
          
        }
        );
      }'''
    

    '''Widget _buildQrView(BuildContext context) {

    ScanPackageBloc _scanPackageBloc =
    ScanPackageBlocProvider.getScanPackageBloc(context);
    SnackbarUtil snackbarUtil = SnackbarUtil();
    snackbarUtil.buildContextItemScan = context;
    
    //var scanArea = (MediaQuery.of(context).size.width < 400 ||
    //        MediaQuery.of(context).size.height < 400) ? 250.0 : 300.0;
    return Padding(
      padding: const EdgeInsets.only(top: 10, bottom: 10),
      child: QRView(
        key: qrKey,
        onQRViewCreated: _scanPackageBloc.updateQRView,
        overlay: QrScannerOverlayShape(cutOutBottomOffset: 0,
            borderColor: Colors.black,
            borderRadius: 10,
            borderLength: 30,
            borderWidth: 5,
            cutOutSize: 250),
      ),
    );
    

    }'''