fluttermobileqr-code

Flutter QR Code Scanner: Camera Continuously Scanning after scan


I have implemented QR Code Scanner using mobile_scanner package in Flutter. and in my app If I scan the QR Code it will navigate me to the result screen. I'm encountering an issue with the QR code scanning functionality in my Flutter app using the mobile_scanner package. After successfully scanning a QR code, the camera continues to scan indefinitely, leading to multiple navigations to the result screen.

Code :

MobileScanner(
        controller: MobileScannerController(
          detectionSpeed: DetectionSpeed.noDuplicates,
        ),
        onDetect: (barcodes) {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) {
                return ResultScreen();
              },
            ),
          );
          print("QR Code Found!");
        },
      )

The expected behavior is that the camera should stop scanning for QR codes once one has been successfully detected and processed. Subsequent appearances of QR codes in front of the camera should not trigger additional navigations to the result screen.


Solution

  • I have used .then after returning to the scanner screen. It's working fine for me If someone still find another solution then go for another answer. here's my code below:

    import 'package:flutter/material.dart';
    import 'package:mobile_scanner/mobile_scanner.dart';
    import 'package:ticket_scanner/Scanner/resultscreen.dart';
    
    class Scanner extends StatefulWidget {
      const Scanner({super.key});
    
      @override
      State<Scanner> createState() => _ScannerState();
    }
    
    class _ScannerState extends State<Scanner> {
      final controller = MobileScannerController(
        formats: const [BarcodeFormat.qrCode],
        detectionSpeed: DetectionSpeed.noDuplicates,
      );
    
      @override
      void initState() {
        controller.start();
        super.initState();
      }
    
      @override
      Future<void> dispose() async {
        controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: [
              MobileScanner(
                controller: controller,
                onDetect: (capture) {
                  Future.delayed(Duration(milliseconds: 500)).then(
                    (value) {
                      final List<Barcode> barcodes = capture.barcodes;
                      for (final barcode in barcodes) {
                        debugPrint('QR Found! ${barcode.rawValue}');
                        Navigator.of(context).push(
                          MaterialPageRoute(
                            builder: (context) {
                              return ResultScreen(
                                  resultText: '${barcode.rawValue}');
                            },
                          ),
                        ).then((value) => controller.start());
                      }
                      controller.stop();
                    },
                  );
                },
              ),
            ],
          ),
        );
      }
    }