W P
W P

Reputation: 91

Flutter Firestore Query Listen - Can't refer to variable from Listen

I have a Query to find the documentID for a specified barcode as such:

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

However, I want to refer to the idOfBarcodeValue outside of the function. I'm trying to find a way to pass that to another function to pass into a SimpleDialog.

Presently, nothing outside of the function recognizes it. It does work, the print verifies.

Here is the scan function that is also being performed:

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

Upvotes: 1

Views: 871

Answers (2)

Daud Codes
Daud Codes

Reputation: 1

I think i should revision the code from Richard Heap, This is my latest code about barcode scan in 7 March 2020, you can use it for barcode scan if you want to get where value from barcode

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

Upvotes: 0

Richard Heap
Richard Heap

Reputation: 51682

snapshots() returns a Stream<Query>. In the same way that you would await a function returning Future, you can await for all of the values (could be zero, one or more) that a Stream produces, for example:

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

Upvotes: 1

Related Questions