flutter - 如何从Firestore文档的一个字段中获取数据?

标签 flutter dart google-cloud-firestore

因此,我正在开发一个应用程序,该应用程序扫描条形码并在Firestore数据库中搜索ID与扫描的条形码匹配的文档,然后将数据添加到 map 列表中。

但是,由于我不知道如何将文档中仅一个字段的值分配给映射中的正确键,我陷入了困境

以下是相关代码:

Future scan() async {
    try {
      String barcode = await BarcodeScanner.scan();
    var databaseSearchResult = firestore.collection("packages").document(barcode);
    String databaseID =
        firestore.collection("packages").document(barcode).documentID;
    if (databaseID == barcode) {
      setState(() {
        productList.add({
          "bruh": databaseSearchResult["bruh"],
          "bruh 2": databaseSearchResult["bruh 2"]
        });
      });
    }
    } catch (e) {
      Fluttertoast.showToast(msg: "Object not found in database");
    }
    try {} 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');
    }
  }

一直以来,我们非常感谢您的帮助,如果可能的话,请尽量保持简单,我对 flutter 打和 Dart 还是很陌生的。非常感谢你

最佳答案

您必须获取文档的数据,但此处未做此操作。请尝试以下代码:

Future scan() async {
    try {
      String barcode = await BarcodeScanner.scan();
    var databaseSearchResult = firestore.collection("packages").document(barcode);
    DocumentSnapshot documentData = await databaseSearchResult.get() //read the data from the document
    Map<String,dynamic> dataMap = documentData.data; //this returns the data as a map where keys are the field names and values are the values of that field
    String databaseID =
        firestore.collection("packages").document(barcode).documentID;
    if (databaseID == barcode) {
      setState(() {
        productList.add({
          "bruh": dataMap["bruh"], //you access the value of "bruh" field in your database
          "bruh 2": dataMap["bruh 2"]
        });
      });
    }
    } catch (e) {
      Fluttertoast.showToast(msg: "Object not found in database");
    }
    try {} 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');
    }
  }

关于flutter - 如何从Firestore文档的一个字段中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61288182/

相关文章:

android - 如何将 flutter 的日期选择器值添加到文本字段?

flutter - 如何在 flutter 存储中存储数组

dart - Flutter - 使用 pushReplacementNamed 在路由之间传递值

javascript - 未捕获的 FirebaseError : Function DocumentReference. set() 使用无效数据调用。不支持的字段值:未定义

rest - Flutter 从服务器获取日语字符解码错误

dart - 我们应该混淆 Flutter 应用程序中的 Dart 代码吗?

javascript - 无法从 React-Native-Firebase(v6) Firestore : undefined is not a function (near '...this._firestore.native.collectionGet...' ) 获取数据

firebase - Firestore : how to read efficiently with a large amount of data

firebase - 如何在 flutter 上使用 firebase 交易正确链接 future ?

dart - js-interop:如何在回调函数中访问输入数据?