Barcode And QRCode Scan In Flutter

Introduction

 
In this article, we will learn how to implement barcode and QRcode scanner feature in Flutter. We will use barcode_scan plugin for that. Barcode_scan plugin is written in Kotlin so we need some extra effort for Android.
 

Output

 
Barcode And QRCode Scan In Flutter
Plugin Required: barcode_scan: ^1.0.0
 

Programming Steps

 
Step 1
 
The first and mostbasic step is to create a new application in Flutter. If you are a beginner in Flutter, then you can check my blog Create a first app in Flutter. I have created an app named as “flutter_barcode_scan”.
 
Step 2
 
For Android, you must do the following before you can use the plugin.
 
Add the camera permission to your AndroidManifest.xml.
  1. <uses-permission android:name="android.permission.CAMERA" />  
Add the BarcodeScanner activity to your AndroidManifest.xml. Do NOT modify the name.
  1. <activity android:name="com.apptreesoftware.barcodescan.BarcodeScannerActivity"/>  
This plugin is written in Kotlin. Therefore, you need to add Kotlin support to your project.
Step 3
 
Edit your project-level build.gradle file to look like this,
  1. buildscript {  
  2.     ext.kotlin_version = '1.3.30'  
  3.     ...  
  4.     dependencies {  
  5.         ...  
  6.         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"  
  7.     }  
  8. }  
  9. ...  
Step 4
 
Edit your app-level build.gradle file to look like this:
  1. apply plugin: 'kotlin-android'  
  2. ...  
  3. dependencies {  
  4.     implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"  
  5.     ...  
  6. }  
Step 5
 
Open the pubspec.yaml file in your project and add the following dependencies into it.
  1. dependencies:  
  2.  flutter:  
  3.    sdk: flutter  
  4.  cupertino_icons: ^0.1.2  
  5.  barcode_scan: ^1.0.0  
Step 6
 
In main.dart, we will implement the floating button to open the camera to scan a barcode. Then, the scanned code will be displayed in the textbox.
 
Following is the implementation of that.
  1. import 'package:flutter/material.dart';  
  2. import 'package:barcode_scan/barcode_scan.dart';  
  3.    
  4. void main() => runApp(MyApp());  
  5.    
  6. class MyApp extends StatelessWidget {  
  7.  @override  
  8.  Widget build(BuildContext context) {  
  9.    return MaterialApp(  
  10.      theme: ThemeData(  
  11.        primarySwatch: Colors.blue,  
  12.      ),  
  13.      home: MyHomePage(),  
  14.    );  
  15.  }  
  16. }  
  17.    
  18. class MyHomePage extends StatefulWidget {  
  19.  @override  
  20.  _MyHomePageState createState() => _MyHomePageState();  
  21. }  
  22.    
  23. class _MyHomePageState extends State<MyHomePage> {  
  24.  String barcode = "";  
  25.  Future scanCode() async {  
  26.    try {  
  27.      String barcode = await BarcodeScanner.scan();  
  28.      setState(() => this.barcode = barcode);  
  29.    } catch (e) {  
  30.      if (e.code == BarcodeScanner.CameraAccessDenied) {  
  31.        setState(() {  
  32.          this.barcode = 'The user did not grant the camera permission!';  
  33.        });  
  34.      } else {  
  35.        setState(() => this.barcode = 'Unknown error: $e');  
  36.      }  
  37.    }  
  38.  }  
  39.    
  40.  @override  
  41.  Widget build(BuildContext context) {  
  42.    return Scaffold(  
  43.      appBar: AppBar(  
  44.        title: Text('Flutter Barcode Scan'),  
  45.      ),  
  46.      body: Center(  
  47.        child: Column(  
  48.          mainAxisAlignment: MainAxisAlignment.center,  
  49.          children: <Widget>[  
  50.            Text(barcode),  
  51.          ],  
  52.        ),  
  53.      ),  
  54.      floatingActionButton: FloatingActionButton(  
  55.        onPressed: scanCode,  
  56.        tooltip: 'Scan',  
  57.        child: Icon(Icons.scanner),  
  58.      ),  
  59.    );  
  60.  }  
  61. }  
Step 7
 
Great, you are done with Barcode and QRCode Scan implementation in Flutter. Run this project in device or emulator and check the output.
 

Possible Errors

  1. Flutter barcode scan Failed to notify project evaluation listener. > java.lang.AbstractMethodError (no error message)
  2. Android dependency 'androidx.core:core' has different version for the compile (1.0.0) and runtime (1.0.1) classpath. You should manually set the same version via DependencyResolution.

Solution

 
In android/build.grader change the version.
  1. classpath 'com.android.tools.build:gradle:3.3.1'  

Conclusion

 
In this article, we have learned how to implement Barcode and QRCode scanner in Flutter application. This feature is useful for payments, offer scanning, billing, etc.


Similar Articles