如何获取QR码的字符串值并将其存储到Firebase中,以便将其链接到特定用户 [英] How to get the string value of QR code and store it into firebase so it links to specific user

查看:40
本文介绍了如何获取QR码的字符串值并将其存储到Firebase中,以便将其链接到特定用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过QR扫描制作一个忠诚度应用程序,不确定如何获取生成给每个用户的QR码的字符串值,然后将其存储在firebase中,以便它链接到特定用户,然后更新号码在链接到用户集合的子集合中扫描用户QR码的次数.

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

这是我的QRImage,它为每个用户生成QR码,但是我不确定如何将数据值链接到firestore集合.

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.

用户集合 https://gyazo.com/803b3ba624a431774ec59f45c1566185

积分收集 https://gyazo.com/3d284e344883e85783bedb23a7cff9cc

推荐答案

尝试一下

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

这篇关于如何获取QR码的字符串值并将其存储到Firebase中,以便将其链接到特定用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆