Closed sahiram036 closed 1 year ago
+1
see this comment, its helped me alhamdullah ..
void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.resumeCamera(); //<--- add this line.
controller.scannedDataStream.listen((scanData) {
setState(() {
result = scanData;
});
});
}
see this comment, its helped me alhamdullah ..
void _onQRViewCreated(QRViewController controller) { this.controller = controller; controller.resumeCamera(); //<--- add this line. controller.scannedDataStream.listen((scanData) { setState(() { result = scanData; }); }); }
Thank you working like charm !!
`void _onQRViewCreated(QRViewController controller) { setState(() { this.controller = controller; resuecamara(); }); controller.scannedDataStream.listen((scanData) { setState(() { result = scanData; print(result!.code); }); }); }
void resuecamara() { if (Platform.isAndroid) { controller!.pauseCamera(); } controller!.resumeCamera(); }`
Describe the bug I'm implementing a in-app QR scanner, Its only working once and then camera is not opening and I got a black screen.. and some logs listed below...
Flutter information ════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown building FutureBuilder(dirty, state: _FutureBuilderState#50d2d):
Failed assertion: boolean expression must not be null
The relevant error-causing widget was FutureBuilder
lib\…\dine_in\qr_scan.dart:114
When the exception was thrown, this was the stack#0 _QRScanScrenState.build.lib\…\dine_in\qr_scan.dart:119
1 _FutureBuilderState.buildpackage:flutter/…/widgets/async.dart:615
2 StatefulElement.buildpackage:flutter/…/widgets/framework.dart:4919
3 ComponentElement.performRebuildpackage:flutter/…/widgets/framework.dart:4806
4 StatefulElement.performRebuildpackage:flutter/…/widgets/framework.dart:4977
5 Element.rebuildpackage:flutter/…/widgets/framework.dart:4529
6 ComponentElement._firstBuildpackage:flutter/…/widgets/framework.dart:4787
(elided 3 frames from dart:async) ════════ Exception caught by widgets library ═══════════════════════════════════ Failed assertion: boolean expression must not be null The relevant error-causing widget was FutureBuilder
lib\…\dine_in\qr_scan.dart:114
════════════════════════════════════════════════════════════════════════════════
[log] 2022-12-22T12:59:25.617752_onPermissionSet true
Additional context `import 'dart:convert'; import 'dart:developer';
import 'package:flutter/material.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; import 'package:toast/toast.dart'; import 'package:app/custom/box_decorations.dart'; import 'package:app/custom/toast_component.dart'; import 'package:app/my_theme.dart'; import 'package:app/screens/new_query_screen.dart';
class QRCodeScren extends StatefulWidget { const QRCodeScren({Key key}) : super(key: key);
@override State createState() => _QRCodeScrenState();
}
class _QRCodeScrenState extends State {
Barcode result;
var resultCode = [];
QRViewController controller; final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
@override void reassemble() { super.reassemble(); // if (Platform.isAndroid) { // controller.resumeCamera(); // } controller.pauseCamera(); controller.resumeCamera(); }
onPressContinue() async { ...............// CODE }
@override Widget build(BuildContext context) { return Scaffold( body: Column( children:[
Expanded(
flex: 5,
child: _buildQrView(
context,
),
),
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (result != null)
Container(
width: 220,
padding: EdgeInsets.only(right: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
resultCode.toString(),
style: TextStyle(
fontSize: 14,
color: MyTheme.accent_color,
fontWeight: FontWeight.w500,
),
),
SizedBox(
height: 16,
),
],
),
)
else
Container(
width: 200,
child: Center(
child: Row(
children: [
Text(
'Scan QR Code',
),
],
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(
children: [
FloatingActionButton.small(
onPressed: () async {
await controller?.toggleFlash();
setState(() {});
},
child: FutureBuilder(
future: controller?.getFlashStatus(),
builder: (context, snapshot) {
// return Text('Flash: ${snapshot.data}');
return Icon(
snapshot.data
? Icons.flashlight_off_sharp
: Icons.flashlight_on_sharp,
);
},
),
),
SizedBox(
width: 26,
),
FloatingActionButton.small(
onPressed: () async {
if (result != null) {
Navigator.pop(context);
} else {
await controller?.resumeCamera();
}
},
child: FutureBuilder(
future: controller?.getFlashStatus(),
builder: (context, snapshot) {
// return Text('Flash: ${snapshot.data}');
return Icon(
result != null
? Icons.close
: Icons.qr_code,
);
},
),
),
],
),
SizedBox(
height: 10,
),
if (result != null)
Container(
height: 30,
width: 120,
decoration: BoxDecorations.buildBoxDecoration_1(),
child: FlatButton(
padding: EdgeInsets.zero,
color: MyTheme.amber,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
side: BorderSide(
color: MyTheme.golden,
),
),
onPressed: () {
onPressContinue();
},
child: Text(
"Continue",
style: TextStyle(
fontSize: 12,
color: MyTheme.golden,
fontWeight: FontWeight.w700,
),
),
),
),
],
),
],
)
],
),
),
)
],
),
);
}
Widget _buildQrView(BuildContext context) { // For this example we check how width or tall the device is and change the scanArea and overlay accordingly. var scanArea = (MediaQuery.of(context).size.width < 400 || MediaQuery.of(context).size.height < 400) ? 250.0 : 400.0; // To ensure the Scanner view is properly sizes after rotation // we need to listen for Flutter SizeChanged notification and update controller return QRView( key: qrKey, onQRViewCreated: _onQRViewCreated, overlay: QrScannerOverlayShape( borderColor: Colors.red, borderRadius: 10, borderLength: 30, borderWidth: 10, cutOutSize: scanArea), onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p), ); }
void _onQRViewCreated(QRViewController controller) { setState( () { this.controller = controller; }, ); controller.scannedDataStream.listen( (scanData) async { try { result = scanData; resultCode = result.code.toString().split(",");
}
void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) { log('${DateTime.now().toIso8601String()}_onPermissionSet $p'); if (!p) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text( 'No Permission', ), ), ); } }
@override void dispose() { controller.dispose(); super.dispose(); } }
`