Flutter:我无法从Firestore集合中获取文档...为什么?

时间:2019-09-23 11:23:57

标签: asynchronous flutter google-cloud-firestore

我真的不明白这一点...请帮助!

我有一个很棒的应用程序,可以扫描QR码并返回一个字符串(barcode)。现在,我想使用该字符串作为输入来进行Firestore查询。这是我的代码

Future barcodeScanning() async {
      try {
        String barcode = await BarcodeScanner.scan();
        getData(barcode);
      } catch (e) {
        setState(() => barcodeFinal = 'Unknown error: $e');
      }
    }

void getData(String zz) {

      DocumentReference documentReference = Firestore.instance.collection('books').document(zz);
      documentReference.get().then((datasnapshot) {
        if (datasnapshot.exists) {
          print(datasnapshot.data['name'].toString());
        } else {
          print("No such book");
        }
      });

这是行不通的,即使文档一直存在于Firestore集合中,我也总是一直以“没有这样的书”结尾。有趣的是,如果我写getData('theNameOfTheDocument');,它就可以工作。我相信异步和处理未来价值一定有问题,但是我可以找出...

这是数据库结构 enter image description here

0 个答案:

没有答案