How to creat QR Code Scanner in Flutter?

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


This content originally appeared on DEV Community and was authored by DEV Community

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


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-09T17:20:56+00:00) How to creat QR Code Scanner in Flutter?. Retrieved from https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/

MLA
" » How to creat QR Code Scanner in Flutter?." DEV Community | Sciencx - Wednesday March 9, 2022, https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/
HARVARD
DEV Community | Sciencx Wednesday March 9, 2022 » How to creat QR Code Scanner in Flutter?., viewed ,<https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/>
VANCOUVER
DEV Community | Sciencx - » How to creat QR Code Scanner in Flutter?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/
CHICAGO
" » How to creat QR Code Scanner in Flutter?." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/
IEEE
" » How to creat QR Code Scanner in Flutter?." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/. [Accessed: ]
rf:citation
» How to creat QR Code Scanner in Flutter? | DEV Community | Sciencx | https://www.scien.cx/2022/03/09/how-to-creat-qr-code-scanner-in-flutter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.