Flutter扫码页面返回报错:'_debugLifecycleState == _StateLifecycle.ready'不成立
解决Flutter扫码页面返回时的'_debugLifecycleState == _StateLifecycle.ready'错误
错误的核心原因是你在onWillPop回调里手动调用了super.dispose(),这违反了Flutter的State生命周期规则——dispose()方法只能由Flutter框架自动调用,手动调用会导致State的生命周期状态异常,触发断言错误。
另外,相机资源的释放应该放在State的dispose()方法中(这是处理资源清理的标准生命周期钩子),而不是仅在返回时处理,这样能确保页面销毁时相机资源被正确释放,避免潜在的内存泄漏。
修改方案:
- 移除
onWillPop中的super.dispose()调用 - 重写State的
dispose()方法,在其中停止并释放相机控制器 - 优化
onWillPop逻辑,仅负责停止相机并返回
修改后的完整代码:
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(); // 替换!为?避免空指针异常 } } @override void dispose() { controller?.stopCamera(); // 停止相机 controller?.dispose(); // 释放控制器资源 super.dispose(); // 仅在框架允许的生命周期钩子中调用super.dispose() } @override Widget build(BuildContext context) { return WillPopScope( onWillPop: () async { await controller?.stopCamera(); // 返回前停止相机 return true; // 允许返回上一页 }, child: Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Container( margin: const EdgeInsets.only(top: 60), child: Center( child: Text( 'Scan QR Code', style: GoogleFonts.epilogue( textStyle: const TextStyle( fontSize: 16, fontWeight: FontWeight.w700, color: cardcolor2, fontStyle: FontStyle.normal, ), ), ), ), ), Container( margin: const 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: const TextStyle( fontSize: 14, fontWeight: FontWeight.w500, color: Colors.black, fontStyle: FontStyle.normal, ), ), ), ), ), Container( margin: const EdgeInsets.only(top: 27), color: Colors.blue, height: 400, child: QRView( key: qrKey, onQRViewCreated: _onQRViewCreated, ), ), Container( margin: const EdgeInsets.only(top: 36), child: Text( 'Problem to scan QR?', textAlign: TextAlign.center, style: GoogleFonts.epilogue( textStyle: const TextStyle( fontSize: 14, fontWeight: FontWeight.w500, color: Colors.black, fontStyle: FontStyle.normal, ), ), ), ), Container( margin: const EdgeInsets.only(top: 5), child: Text( 'Click Here', textAlign: TextAlign.center, style: GoogleFonts.epilogue( textStyle: const 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; }); }); } }
额外优化点:
- 将
controller!替换为controller?,避免控制器未初始化时触发空指针异常 - 给所有
EdgeInsets添加const修饰,减少不必要的对象创建,提升性能
内容的提问来源于stack exchange,提问作者Danish Ghazi
相关产品推荐
相关产品推荐

