Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter BarCode to check if data contains Letters/Word with QR Code Scanner

Hi I have been trying to read the QR Code and then navigate to a page when scan data contains specific keyword. Currently, I did print the data and it shows correctly, but when I want pass it to check with startsWith then it show the error below. I have tried using contains as well (Same Error), I did check the value is null before applying, and I also add ? before initializing Barcode. The code is edited from the example of packages qr_code_scanner. The related code I feel like is only here. I am not sure what am I missing now to remove this error. Please help.

void _onQRViewCreated(QRViewController controller) {
    setState(() {
      this.controller = controller;
    });
    controller.scannedDataStream.listen((Barcode? scanData) {
      setState(() {
        Barcode? result;
        result = scanData;
        if (result != null){
          if(result!.code.startsWith("anything")){
            \\Navigate here
          }
        }
        
      });
    });
  }

I am getting error shown below from result!.code.startsWith("anything")

The method 'startsWith' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

change the condition like this

if(result !=null && result.code!=null){
   if (result!.code!.startsWith("anything")) {
       // do you navigation here
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading