颤振:"_debugLifecycleState == _StateLifecycle.ready":不正确



我正在尝试开发一个可以读取qr码的扑动移动应用程序。但是每次我按下模拟器上的后退按钮时,它都会给出错误:'_debugLifecycleState == _StateLifecycle。Ready ':不正确。

代码如下:

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;
});
});
}
}

错误如下:错误显示

如果有人能帮助我,我很感激,谢谢

在进入上一页之前应该先关闭相机

您可以覆盖状态类上的dispose方法,而不是在WillPopScope中执行super.dispose();

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(

最新更新