How to make QR Code Scanner in Flutter ?

Dipen Maharjan
1 min readMar 9, 2022

In this post, we’ll be using package barcode_scan2: ^4.2.0 to scan the QR Code.

Add Package to the Pubspec.yaml

dependencies:
flutter:
sdk: flutter
barcode_scan2: ^4.2.0

Allow Access to Camera in Android and IOS

To use the QR Scanner, we have to allow the app to access the camera phone so at first we’ll add the code to Android and IOS Folder,

go to:

path: android/app/src/main/AndroidManifest.xml

and Add this line of code before application

...
<uses-permission android:name="android.permission.CAMERA" />
<application ...

and Save.

Incase you get the multidex error
please add

path: android/app/src/build.gradle
---------------
defaultConfig {
....
multiDexEnabled true
}
...
...
...
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}

Now Moving on to the IOS Part

path: ios/Runner/Info.plist

add these code

<dict>
<key>NSCameraUsageDescription</key>
<string>Camera permission is required for barcode scanning.</string>
</dict>

Create QR Code Dart

Once you have done that, now create a QRScannerScreen.dart file and add a button in the container or center

TextButton(
child: Text("Scan Now"),
onPressed: (){
var result = await BarcodeScanner.scan(); //barcode scanner
print(result.type); // The result type (barcode, cancelled, failed) print(result.rawContent); // The barcode content
print(result.format); // The barcode format (as enum)
print(result.formatNote);
}
)

Use Barcode: BarCode Scan 2

--

--

Dipen Maharjan

I am a passionate Developer, been working in IT field for more than 4 years. Currently Documenting my journey in Youtube: @ EdwinOslenTV