How to scan QR code with Flutter

First, we’ll need to import the qrcode_reader package from

Add the following to your pubspce.yaml file

qrcode_Reader: any

Create a new Dart file and paste this code:

 import 'package:flutter/material.dart';
 import 'package:qrcode_reader/qrcode_reader.dart';

 class ScanQRCode extends StatefulWidget {
   _ScanQRCodeState createState() => _ScanQRCodeState();

 class _ScanQRCodeState extends State<ScanQRCode> {
   String scannedString = '';

   _scanQR() async {
     setState(() async {
       scannedString = await new QRCodeReader().scan();

   Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(title: Text('Scan QR Code example')),
       body: Center(
         child: scannedString == null || scannedString.isEmpty
             ? RaisedButton(
                 child: Text('Scan QR Code'),
                 onPressed: _scanQR,
             : Text(
                 style: TextStyle(
                   fontSize: 17.0,
                   fontWeight: FontWeight.bold,

In your main.dart file:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch:,
      home: new ScanQRCode();

Add a Comment