mobile_scanner fails when the Flutter QR screen opens a second time
Your error
MobileScannerException(controllerAlreadyInitialized, The scanner was already started.)3 more lines it can showFix it fast
Most common cause, 1 of 3The camera session from the first visit is still running
Keep a single scanner route.
// On the scanner screen, before showing the result page:
await controller.stop();
await Navigator.of(context).push(
MaterialPageRoute(builder: (_) => ResultPage(code: value)),
);
await controller.start();
// On the result page, go back to the existing scanner:
Navigator.of(context).pop();Short answer
MobileScannerException with controllerAlreadyInitialized or controllerDisposed on the second visit to a QR screen means the first visit still holds the camera, or a shared MobileScannerController was disposed and then reused. In mobile_scanner 7.x, create the controller inside the screen's State, dispose it there, and pop back to the scanner instead of pushing another copy.
Which cause do you have?
Log the error code the package gives you. The MobileScanner widget passes the exception to its errorBuilder; if you call start() yourself, catch it there. The code name after the opening bracket picks the cause below.
MobileScanner(
controller: controller,
errorBuilder: (context, error) {
debugPrint('scanner error: ${error.errorCode.name} ${error.errorDetails?.message}');
return Center(child: Text(error.errorCode.name));
},
)Then find the line you see in this list and open its fix:
- 1The camera session from the first visit is still running
controllerAlreadyInitialized with the message The scanner was already started., usually in a release or profile build only - 2A controller shared between visits was disposed when the screen closed
controllerDisposed, and the controller lives outside the scanner screen (a global, a static field, a provider or a service) - 3Your own start() call collides with autoStart
controllerInitializing, and it can fail on the very first visit too
What does the error look like?
MobileScannerException(controllerAlreadyInitialized, The scanner was already started.)MobileScannerException(controllerDisposed, The MobileScannerController was used after it was disposed.)MobileScannerException(controllerInitializing, The MobileScannerController is still initializing. Await the previous call to start() or disable autoStart before starting manually.)Called start() while already started
Cause 1 of 3
The camera session from the first visit is still running
You seecontrollerAlreadyInitialized with the message The scanner was already started., usually in a release or profile build only
Check
controllerAlreadyInitialized on reopening means the camera that the previous visit started was never stopped, so the new start() is refused; the package's own advice for this code is to stop the controller before starting it again. Two patterns cause it: a result page pushes a fresh scanner route on top of the old one, which is still mounted with its camera on, or a controller made with autoStart: false was started by hand and never stopped. Debug builds hide the problem, because in debug mode the widget force-stops the platform camera each time it initializes.
grep -rn "MobileScannerController(" lib/
grep -rn "Navigator.*push" lib/ | grep -i scanFix
Keep a single scanner route. From the result page, pop back to it rather than pushing a new one, and stop the camera while another page covers it. A controller that you start by hand must be stopped or disposed when its screen closes.
// On the scanner screen, before showing the result page:
await controller.stop();
await Navigator.of(context).push(
MaterialPageRoute(builder: (_) => ResultPage(code: value)),
);
await controller.start();
// On the result page, go back to the existing scanner:
Navigator.of(context).pop();Verify
Build a release version, then scan, open the result page, return and scan again several times. The preview comes back each time and errorBuilder stays silent.
flutter run --releaseConfirmed by pub.dev
Cause 2 of 3
A controller shared between visits was disposed when the screen closed
You seecontrollerDisposed, and the controller lives outside the scanner screen (a global, a static field, a provider or a service)
Check
controllerDisposed means code called start() or another method on a MobileScannerController whose dispose() already ran. The package documents that a disposed controller cannot be used again. A screen that disposes a controller it does not own breaks the next visit, which receives the same dead object.
grep -rn "MobileScannerController(" lib/
grep -rn "\.dispose()" lib/ | grep -i scanFix
Let each scanner screen create its own controller in its State and dispose of it in dispose(). If you do not need the controller at all, pass none: the MobileScanner widget then builds an internal controller and disposes of it itself.
class _ScanScreenState extends State<ScanScreen> {
final MobileScannerController controller = MobileScannerController();
@override
void dispose() {
unawaited(controller.dispose()); // import 'dart:async';
super.dispose();
}
@override
Widget build(BuildContext context) =>
MobileScanner(controller: controller, onDetect: _handleBarcode);
}Verify
Open and close the scanner screen five times in a row; the camera starts on every visit and controllerDisposed no longer appears in the log.
flutter runConfirmed by pub.dev
Cause 3 of 3
Your own start() call collides with autoStart
You seecontrollerInitializing, and it can fail on the very first visit too
Check
controllerInitializing means start() was called while an earlier start() on the same controller was still in progress. The controller starts the camera by itself unless autoStart is false, so an extra controller.start() in initState, or in a lifecycle handler added to restart the camera, runs into the automatic one.
grep -rn "autoStart" lib/
grep -rn "controller.start()" lib/Fix
Choose one way to start the camera. Either delete the manual start() calls and keep the default autoStart, or create the controller with autoStart: false and await a single start() yourself, as the package README does when it handles app lifecycle changes.
final MobileScannerController controller = MobileScannerController(
autoStart: false,
);
@override
void initState() {
super.initState();
unawaited(controller.start());
}Verify
The screen opens with a live preview and the log shows no controllerInitializing entry.
flutter runConfirmed by pub.dev
How do I stop it coming back?
- Give every MobileScannerController exactly one owner: the State that creates it is the only place that disposes of it.
- Test the open, close and reopen path in a release or profile build before shipping, since debug mode force-stops the camera whenever the scanner widget initializes and so masks controllerAlreadyInitialized.
- Decide per controller between autoStart and manual start(); when a WidgetsBindingObserver restarts the camera on resume, set autoStart: false.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, Qist Bazaar, SessionBird.