Search code examples
flutterdartbarcode-scanner

Unable to display the contents of a barcode using barcode scanner


I have been trying to display the barcode of an item using the barcode scanner... I have written a code for scanning the barcode but I am unable to display as it throws up an error. This code is capable of first turning on the camera and then scans the barcode till here it works completely fine but when I am trying to display the barcode it throws up an exception.

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';


class CartItems extends StatefulWidget {
  @override
  _CartItemsState createState() => _CartItemsState();
}

class _CartItemsState extends State<CartItems> {

  String barcode = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(child: Text('Code: ' + barcode)),
      floatingActionButton: FloatingActionButton(
        onPressed: barcodeScanning,
        child: Icon(Icons.add),
      ),
    );
  }

  Future barcodeScanning() async {
    try {
      String barcode = BarcodeScanner.scan() as String;
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.cameraAccessDenied) {
        setState(() {
          this.barcode = 'No camera permission!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException {
      setState(() =>
      this.barcode =
      'Nothing captured.');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
    }
  }
}

Please look into the matter I am struggling a lot to resolve the error.


Solution

  • Pass the returned result from your scanner function. This way the function executes and feeds its findings to the main context also add a print() to see the result at the bottom to see what the exception might be in the console

    class CartItems extends StatefulWidget {
      @override
      _CartItemsState createState() => _CartItemsState();
    }
    
    class _CartItemsState extends State<CartItems> {
    
      String barcodeResult = "";
      String barcode = "";
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(child: Text('Code: ' + barcode)),
          floatingActionButton: FloatingActionButton(
            onPressed:(){ barcodeScanning().then((value) 
           {
              setState(()
               {barcodeResult = value;})})}// this should work
            child: Icon(Icons.add),
          ),
        );
      }
    
      Future barcodeScanning() async {
        try {
          String barcode = BarcodeScanner.scan() as String;
          setState(() => this.barcode = barcode);
        } on PlatformException catch (e) {
          if (e.code == BarcodeScanner.cameraAccessDenied) {
            setState(() {
              this.barcode = 'No camera permission!';
            });
          } else {
            setState(() => this.barcode = 'Unknown error: $e');
          }
        } on FormatException {
          setState(() =>
          this.barcode =
          'Nothing captured.');
        } catch (e) {
          setState(() => this.barcode = 'Unknown error: $e');
        }
       return barcode;//this
      }
    }