Home > Net >  How to pass data from a Barcode scanner to a Textfield (Flutter)
How to pass data from a Barcode scanner to a Textfield (Flutter)

Time:11-16

I am using the 'flutter_barcode_scanner' package and I want to pass some data from the barcode scanner to a TextField in the app that I am working on.

I have a TextEditingController like this:

 var searchController = TextEditingController();

And the code I use for the barcode scanner is this:

Future<void> scanBarcodeNormal() async {
    String barcodeScanRes;
    try {
      barcodeScanRes = await FlutterBarcodeScanner.scanBarcode('#ff6666', 'Cancel', true, ScanMode.BARCODE);
      print(barcodeScanRes);
    } on PlatformException {
      barcodeScanRes = 'Cihazınız barkod okumayı desteklemiyor.';
    }
    if (!mounted) return;
    setState(() {
      _scanBarcode = barcodeScanRes;
    });
  }

I know that I need to change some things in the last part,

 if (!mounted) return;
    setState(() {
      _scanBarcode = barcodeScanRes;
    });

I want to replace the setState(() {}) with a code that achieves what I want (Passing the data from the barcode to the TextField.)

I am trying to replace the setState(() {}) of the barcode scanner with a code that passes the data to TextField.

CodePudding user response:

Try replacing this statement

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

With this

searchController.text  = barcodeScanRes;

Gist: https://gist.github.com/SteplerPaulo/cee0e6f03fb8c1b7a5014074f665594b

  • Related