Search code examples
flutterdartbarcodebarcode-scanner

How to Return Different Values When Scanning a Barcode?


I am having an issue with returning two different values when scanning a barcode. Currently, when I scan the barcode, the same value is returned for both Text widgets. I need each Text widget to take a different value. How can I achieve this?

Screenshot: Barcode Scan

BARCODE

 String? scanResult;

 Future scanBarcode() async {
    String scanResult;
    try{

    scanResult = await FlutterBarcodeScanner.scanBarcode("#ff6666", "Cancel", true, ScanMode.BARCODE);
    } on PlatformException{
      scanResult = 'Failed to get platform verion';
    }
    if (!mounted) return;
    setState(() => this.scanResult = scanResult);
  } 

My Code for show Scan-Results

  Text(
                    '$scanResult',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

 Text(
                    '$scanResult',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ), 

My Question : As shown in the attached image. I have two values in the data section. How to display both data separately in two widgets?


Solution

  • you must capture and separate the result using split

    Text(
                        '${scanResult.split('\n')[0]}',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 24.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
    
     Text(
                        '${scanResult.split('\n')[1]}',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 24.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),`