Closed arnabonetraker closed 2 months ago
Not quite. In my case scanner keep detecting barcode even while navigating to other screen(no scanner initialised in this second screen).
In my case after navigation I do cameraController.stop(),
but when I return to scan page camera is not working.
In my case after navigation I do
cameraController.stop(),
but when I return to scan page camera is not working.
did you set your MobileScannerController(autoStart:true)
?
If not then you will have to cameraController.start()
when you pop and get the callback.
In my case after navigation I do
cameraController.stop(),
but when I return to scan page camera is not working.did you set your
MobileScannerController(autoStart:true)
? If not then you will have tocameraController.start()
when you pop and get the callback.
autoStart:true
stay automaticly, I tried to pass controller to the new Screen and call cameraController.start()
, but it's not working
@denihero I think the problem is in the controller. I managed, Overwriting the controller every time it wants to scan again. Try this:
Declare the controller so you don't have to check for null in functions:
MobileScannerController cameraController = MobileScannerController();
When I want to stop the scanner:
FutureOr<void> _onQrViewCameraStopped( QrViewCameraStopped event, Emitter<QrViewState> emit) { cameraController.stop(); cameraController.dispose(); }
When I want to scan a qr:
FutureOr<void> _onQrViewLoaded( QrViewLoaded event, Emitter<QrViewState> emit) { cameraController = MobileScannerController(); emit(QrViewLoad(event.plants)); }
@denihero I think the problem is in the controller. I managed, Overwriting the controller every time it wants to scan again. Try this:
Declare the controller so you don't have to check for null in functions:
MobileScannerController cameraController = MobileScannerController();
When I want to stop the scanner:
FutureOr<void> _onQrViewCameraStopped( QrViewCameraStopped event, Emitter<QrViewState> emit) { cameraController.stop(); cameraController.dispose(); }
When I want to scan a qr:
FutureOr<void> _onQrViewLoaded( QrViewLoaded event, Emitter<QrViewState> emit) { cameraController = MobileScannerController(); emit(QrViewLoad(event.plants)); }
Where did you get QrViewLoaded
and QrViewState
?
Am using Bloc on my proyect
In case people still dealing with this issue, you can do like below :
controller.stop();
Navigator.pushNamed(context, 'someRoute').then((value) {
controller.start();
});
if this wasn't enough, do : Navigator.pop(context, true)
in second page.
No need to bloc or etc.
thanks, man, it's working, I'll appreciate it
So as I see at the moment there is no option to pop back from the Scanner page and later use the Scanner again? I tried different approaches, stopping and starting the controller manually or completely dispose the controller. But I couldn't find a way getting the Scanner run again after leaving it ones (except leaving it forward (push) and afterwards going back like @braveshine1993m recommendet. I tested on an iPhone 12 ios 16.5.1 and the android emulator.
Edit: For me this approch lets me use the scanner how i need it: [https://github.com/juliansteenbakker/mobile_scanner/issues/589#issuecomment-1599677867]
Note: This is just a temporary solution
If you are using flutter bloc, then you can use that to solve this issue. Create a Cubit for Scanner View, and add controller in Cubit.
class ScannerCubit extends Cubit<ScannerState> {
ScannerCubit() : super(ScannerInitial());
MobileScannerController controller =
MobileScannerController(autoStart: false);
late bool _killCamera = true;
Future<void> start() async {
_killCamera = false;
if (!controller.isStarting) {
await controller.start();
}
controller.startArguments.addListener(_cameraEvent);
}
void _cameraEvent() async {
if (_killCamera) {
await controller.stop();
controller.dispose();
}
}
Future<void> stop() async {
_killCamera = true;
await controller.stop();
controller.dispose();
}
}
How to use in View
class _ScannerState extends State<Scanner> {
@override
void initState() {
WidgetsFlutterBinding.ensureInitialized();
BlocProvider.of<ScannerCubit>(context).start();
super.initState();
}
@override
void dispose() {
BlocProvider.of<QrScannerCubit>(context).stop();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Scanner"),
),
body: MobileScanner(
controller: BlocProvider.of<QrScannerCubit>(context).controller,
onDetect: (value) {},
),
);
}
}
Call dartBlocProvider.of<QrScannerCubit>(context).stop()
in other Views, it will stop any active camera instance.
for example
class _HomeState extends State<Home> {
@override
void initState() {
BlocProvider.of<QrScannerCubit>(context).stop();
super.initState();
}
@override
Widget build(BuildContext context) {
return const Center(
child: Text("HOME"),
);
}
}
With the new beta release, version 5.0.0-beta.1, the lifecycle of the MobileScannerController is now user managed.
So I'm going to close this issue.
My flow requires when barcode is detected navigate to new screen, do some activity then pop to scanner screen again. Now the issue is the scanner is active in background, so it is detecting the barcode again even when I am in a different screen.
_mobileScannerController = MobileScannerController(facing: CameraFacing.back, formats: [BarcodeFormat.code128], detectionSpeed: DetectionSpeed.normal, autoStart: true, )
This is my controller settings. I have tried stoping the camera usingstop()
before navigating to another screen. then using callback tostart()
again when it is poped. This works for android but it is not working in a IOS device. For IOS the scanner is still activate in the background.