【问题标题】:Flutter Firestore Query Listen - Can't refer to variable from ListenFlutter Firestore Query Listen - 无法从 Listen 中引用变量
【发布时间】:2018-07-17 00:09:15
【问题描述】:

我有一个查询来查找指定条形码的 documentID,如下所示:

Future findBarcode() async {
  String searchBarcode = await BarcodeScanner.scan();

Firestore.instance.collection('${newUser.userLocation}').where('barcode', isEqualTo: '${searchBarcode}').snapshots().listen(
(data) { 
  String idOfBarcodeValue = data.documents[0].documentID;
  print(idOfBarcodeValue);
     }
  );  
}    

但是,我想在函数之外引用 idOfBarcodeValue。我正在尝试找到一种方法将其传递给另一个函数以传递给 SimpleDialog。

目前,函数之外的任何东西都无法识别它。它确实有效,打印验证。

下面是正在执行的扫描功能:

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

【问题讨论】:

    标签: search dart google-cloud-firestore flutter


    【解决方案1】:

    snapshots() 返回一个Stream<Query>。与await 一个返回Future 的函数一样,您可以await for Stream 生成的所有值(可以是零、一个或多个),例如:

    Future findBarcode() async {
      String searchBarcode = await BarcodeScanner.scan();
    
      String idOfBarcodeValue;
      Stream<Query> stream = Firestore.instance
          .collection('${newUser.userLocation}')
          .where('barcode', isEqualTo: '${searchBarcode}')
          .snapshots();
      await for (Query q in stream) {
        idOfBarcodeValue = q.documents[0].documentID;
      }
    
      print(idOfBarcodeValue);
      // if the stream had no results, this will be null
      // if the stream has one or more results, this will be the last result
      return idOfBarcodeValue;
    }
    

    【讨论】:

    • 这对我不起作用。 print(idOfBarcodeValue) 仍然没有被调用。与初始方法相同的问题。如果,我将 print 语句移到 await 方法中,它就可以工作。虽然不能满足我的需要。
    • 如果您在 await for 循环中添加打印,您会看到什么?如果你在循环之后添加print('here');,你什么时候看到?
    • 如果我在循环内打印,我会看到。如果我在循环之外打印 - 什么也不会发生。好像在那之前就断了。不知道为什么。
    • 我最终要做的是将 idOfBarcodeValue 传递给 Navigator.push() 并作为值传递给新视图。
    • 你有没有看到循环后的print('here');?或者,如果你将context 传递给findBarcode,你可以在await for 循环中调用Navigator.push 吗?
    【解决方案2】:

    我认为我应该修改 Richard Heap 的代码,这是我在 2020 年 3 月 7 日关于条码扫描的最新代码,如果您想从条码中获取价值,可以将其用于条码扫描

    String barcode = "";
    Future scan() async {
    try {
      barcode = await BarcodeScanner.scan();
      setState(() => this.barcode = barcode);
    
      String idOfBarcodeValue;
      Stream<QuerySnapshot> stream = dbReference
          .collection('userLocation')
          .where('qrCodeNumber', isEqualTo: barcode)
          .snapshots();
      await for (QuerySnapshot q in stream) {
        idOfBarcodeValue = q.documents[0].documentID;
        print(idOfBarcodeValue);
      }
    
    } 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 = 'User after click "back"-button before result');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 2021-09-04
      • 2021-07-18
      • 1970-01-01
      • 2015-10-15
      • 2018-06-22
      • 2013-04-30
      相关资源
      最近更新 更多