How to get the string value of QR code and store it into firebase so it links to specific user

爷,独闯天下 提交于 2021-01-28 09:15:59

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/61233434/how-to-get-the-string-value-of-qr-code-and-store-it-into-firebase-so-it-links-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!