首页 > 解决方案 > 如何获取 QR 码的字符串值并将其存储到 firebase 以便链接到特定用户

问题描述

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

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 集合。

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

这是我在不同页面上的扫描功能。

用户收藏 https://gyazo.com/803b3ba624a431774ec59f45c1566185

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

标签: firebasefluttergoogle-cloud-firestoreqr-code

解决方案


尝试这个

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

推荐阅读