I am building a QR scanner to help organize a collection. Basic data about the items is encoded into a json string then into a QR code. The generation of such labels programatically was successful. The next phase was to create a simple visor in flutter. The chosen library was qr_code_scanner. My simple app was to be a scanner which when detecting a valid QR code (one containing a json string describing the required structures) would redirect into another screen where data was displayed. The scanner detects the QR codes, the objects are parsed and a widget containing another scaffold is pushed. This is mostly what I want however an issue was detected: when a valid QR was detected the app would push the data display screen multiple times, sometimes as many as 9, thus breaking backward navigation.
The method responsible of handling the event is named "onQRViewCreated" (like in the example).
void onQRViewCreated(QRViewController controller){
setState(() => this.controller = controller);
controller.scannedDataStream.listen(
(qrData) {
setState(
() {
barcode = qrData;
if (barcode?.format == BarcodeFormat.qrcode) {
try {
Item item = Item.fromJSon(
jsonDecode(barcode?.code ?? "")
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ItemDisplay(
key: const Key("item"),
item: item
)
)
);
}
on FormatException {
Fluttertoast.showToast(msg: "Invalid QR Code!");
}
on Exception {
Fluttertoast.showToast(msg: "Error!");
}
}
}
);
}
);
}
I would like to be able to push the ItemDisplay only once but I don't know how to do it.
Thank you in advance
CodePudding user response:
Put await controller.pauseCamera();
before the Navigator.push
CodePudding user response:
I would suggest to use mobile_scanner package, which is a newer version from the same author. When creating MobileScanner
widget of this package, there is an option allowDuplicates
, which you can set to false
to avoid this behaviour.