1

I'm having a problem getting a variable after the function runs.

I have to read a qr code and then put the qr code in a text field, but when i try to read it gets the default variable text, and only gets the right qr code when i try to read for the seccond time.

Here is the qr reader function:

  Future<void> scanQR() async {
    String barcodeScanRes;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
          '#ff6666', 'Cancel', true, ScanMode.QR);
      print(barcodeScanRes);
    } on PlatformException {
      barcodeScanRes = 'Failed to get platform version.';
    }
    if (!mounted) return;

    setState(() {
      _scanBarcode = barcodeScanRes;
    });

  }

Here the button to run the function and then put the qr on the controller:

       onPressed: () {
                              scanQR();
                           
                                setState(() {
                                  _codprodController.text = _scanBarcode;
                                });                                                    
                            },
//on the first time i try it shows "unknown" that is the _scanBarCode default text instead of the qr i read

I guess the problem is the "_codprodController.text = _scanBarcode;" that its not runnig after (but together) the scanQR() but i dont know hot to fix.

1 Answer 1

1

Try below

  onPressed: () async {
                              await scanQR();
                           
                                setState(() {
                                  _codprodController.text = _scanBarcode;
                                });                                                    
                            },
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, so easy, i just forgot it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.