-
-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Camera does not work after navigation from one screen to another (Both use MobileScanner) #589
Comments
@oleksii-tatarintsev have you found any workaround? I'm in the same exact situation |
Have you tried with what mentioned in this other issue? #539 (comment) @override
void initState() {
controller.stop(); ---> call stop here
super.initState();
} |
I was just running into a very similar issue and have found a workaround that works for me. instead of defining the MobileScannerController in the Scanner class definition, pass it through as a parameter upon instantiation. this example code may be easier to understand. class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
children: [
TextButton(
child: Text("BARCODE SCANNER"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ScannerScreen(
controller: MobileScannerController(
autoStart: true,
),
),
),
);
},
),
],
),
),
);
}
}
class ScannerScreen extends StatelessWidget {
const ScannerScreen({
super.key,
required this.controller,
});
final MobileScannerController controller;
@override
Widget build(BuildContext context) {
return MobileScanner(
onDetect: (data) {
controller.stop();
Navigator.pop(context);
},
controller: controller,
);
}
} as you can see, I create a new controller each time the ScannerScreen is navigated to, and pass it through as a parameter of ScannerScreen. |
None of the workarounds seemed to work. What ended up working was adding a delay between stopping and starting the controller:
Not a great solution, but atleast it works. Im running this on a OnePlus Nord N100 (cheap, old phone) in debug mode. Im guessing that the timeout can be shorter on a newer phone running a production build. |
I think you could just await the controller to stop
|
In my case, when scanning the QR I go to a new screen where I perform an action and make a pop to return to the QR scan.
|
Every time when i am trying to navigate from one screen to another (both screens use MobileScanner), on the second screen i receive white screen if i do not use autostart and starting/stopping controller by myself. If i use autostart, i receive black screen
Package: mobile_scanner: 3.2.0
iOS Device: iPad Pro 12.9 inch (5th generation)
iPadOS: 16.4
Android Device: Samsung Galaxy Tab S7
Android version: Android 11
Code:
Console: output:
Actual result when autostart false:
Actual result when autoStart true:
Flutter doctor:
RPReplay_Final1681298831.mov
The text was updated successfully, but these errors were encountered: