I'm trying to make a loyalty app by QR scan and unsure how I can get the string value of the QR code generated to each user and then store it in firebase so that it links to a specific user then update the number of times the user's QR code has been scanned in a sub-collection linked to the user collection.
QrImage(
data: '${user?.uid}',
version: QrVersions.auto,
size: 300,
errorStateBuilder: (cxt, err) {
return Container(
child: Center(
child: Text('Error',
textAlign: TextAlign.center,
style: new TextStyle(color: Colors.red),
),
),
);
},
),
This is my QRImage which generates the QR code to each user but I'm unsure how to link the data value to the firestore collection.
Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException{
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}
And this is my scan function which is on a different page.
User collection https://gyazo.com/803b3ba624a431774ec59f45c1566185
Points collection https://gyazo.com/3d284e344883e85783bedb23a7cff9cc
Try this
Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() => this.barcode = barcode);
print("scanned sucsessfully");
//plus one to points when scanned
String userId = (await FirebaseAuth.instance.currentUser()).uid;
final CollectionReference pointsCollection = Firestore.instance.collection("users");
await pointsCollection.document(userId).collection('points').document(userId)
.updateData({
"points": FieldValue.increment(1),
"transactions": FieldValue.increment(-1)
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException{
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}