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.
2
Answers
I’d recommend to initiate a controller like this:
And then you have 2 options:
either call dispose() or stop()
So once you push a user on a new screen then stop the controller. If a user goes back call start()
The problem you are facing is caused by not disposing the controller
You have dispose your controller once get the data from QR.