I am trying do develop a flutter mobile app that can read a qr code. But everytime I press the back button on the emulator it gives the error: '_debugLifecycleState == _StateLifecycle.ready': is not true.
Below is the code:
class QRScan extends StatefulWidget {
const QRScan({Key? key}) : super(key: key);
@override
State<QRScan> createState() => _QRScanState();
}
class _QRScanState extends State<QRScan> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
Barcode? result;
QRViewController? controller;
@override
void reassemble() {
super.reassemble();
if (Platform.isAndroid) {
controller!.pauseCamera();
} else if (Platform.isIOS) {
controller!.resumeCamera();
}
}
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
await controller?.stopCamera();
super.dispose();
return true;
},
child: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
margin: EdgeInsets.only(top: 60),
child: Center(
child: Text(
'Scan QR Code',
style: GoogleFonts.epilogue(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
color: cardcolor2,
fontStyle: FontStyle.normal,
)
),
),
),
),
Container(
margin: EdgeInsets.only(top: 20, left: 50, right: 64),
child: Center(
child: Text(
'Scan QR Code on kiosk to directly call the customer support',
textAlign: TextAlign.center,
style: GoogleFonts.epilogue(
textStyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Colors.black,
fontStyle: FontStyle.normal,
)
),
),
),
),
Container(
margin: EdgeInsets.only(top: 27),
color: Colors.blue,
height: 400,
child: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
),
Container(
margin: EdgeInsets.only(top: 36),
child: Text(
'Problem to scan QR?',
textAlign: TextAlign.center,
style: GoogleFonts.epilogue(
textStyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Colors.black,
fontStyle: FontStyle.normal,
)
),
),
),
Container(
margin: EdgeInsets.only(top: 5),
child: Text(
'Click Here',
textAlign: TextAlign.center,
style: GoogleFonts.epilogue(
textStyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Colors.black,
fontStyle: FontStyle.normal,
)
),
),
),
],
),
),
);
}
void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
setState(() {
result = scanData;
});
});
}
}
And here is the error: Error shown
Appreciated if someone could help me cheers
It should close the camera first before going to previous page.
Instead of doing super.dispose();
inside WillPopScope
, you can override the dispose method on state class.
class _QRScanState extends State<QRScan> {
.....
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
await controller?.stopCamera();
//not here
return true;
},
child: Scaffold(