How To

Flutter Barcode Scanner: Step-by-Step Tutorial (2026)

Build a Flutter barcode scanner for Android and iOS with the barkoder_flutter plugin, from flutter create to a working scan screen, based on the official docs.

Tutorial cover: Build a Barcode Scanner App with Flutter and barKoder SDK
On this page

This tutorial builds a Flutter barcode scanner for Android and iOS with the barkoder_flutter plugin from the barKoder barcode scanner SDK. You'll go from flutter create to a screen with a camera preview, Start and Stop buttons and the decoded result. The code follows the barKoder Flutter documentation, including the Android and iOS settings a new project needs.

Quick answer: Run flutter pub add barkoder_flutter, add the Gradle lines from Step 3 if your Android build stops at checkDebugAarMetadata, set the iOS platform to 15.0 in ios/Podfile, add NSCameraUsageDescription to ios/Runner/Info.plist, put a BarkoderView with your license key in your widget tree, enable barcode types in onBarkoderViewCreated, and call startScanning with a callback that reads result.decoderResults[0].textualData.

What you'll build

A BarcodeScanner widget with the camera preview, Start Scanning and Stop Scanning buttons, and the last result under the preview. It reads Code 128, Code 39 and EAN-13 out of the box, and you can turn on any other type.

Before you start

  • Install the Flutter SDK and set up your environment for Android and iOS.
  • Get a license key: generate a free trial key. Choose Native and enter your app ID (the Android package name or iOS bundle ID). A trial key lasts 30 days, on up to 25 devices.

Step 1: Create a project

Create a new app with flutter create my_app, then cd my_app. The steps below run in that folder.

Step 2: Add barkoder_flutter

Run flutter pub add barkoder_flutter, which adds the latest version, or add the package to the dependencies in pubspec.yaml yourself:

dependencies:
  flutter:
    sdk: flutter
  barkoder_flutter: <package version>

Then run flutter pub get to fetch it.

Step 3: Fix the Android build (barkoder_flutter 1.7.2)

With barkoder_flutter 1.7.2, the Android build of a new app (Flutter 3.47) stops at checkDebugAarMetadata (":barkoder_flutter is currently compiled against android-31"): the plugin compiles against Android API 31, and the AndroidX libraries a Flutter app uses need 34 or later. Add these lines at the top of android/build.gradle.kts; they compile the plugin against API 36, as the app is:

subprojects {
    afterEvaluate {
        if (name == "barkoder_flutter") {
            extensions.configure<com.android.build.api.dsl.LibraryExtension>("android") {
                compileSdk = 36
            }
        }
    }
}

The plugin needs Android 5.0 (API 21) or later. A new Flutter project already sets a higher minimum (minSdk = flutter.minSdkVersion). If your project sets it to 20 or lower, raise it in android/app/build.gradle.kts:

android {
    defaultConfig {
        minSdk = 21
    }
}

Step 4: Set iOS 15 and allow the camera

barkoder_flutter 1.7.2 needs iOS 15 or later. In ios/Podfile, set the platform line (a new project has it commented out):

platform :ios, '15.0'

On Android, the plugin's manifest asks for the camera permission. On iOS, add the camera key to ios/Runner/Info.plist:

<key>NSCameraUsageDescription</key>
<string>Camera permission</string>

Step 5: Create the scanner widget

Create lib/barcode_scanner.dart with this code, based on the General example in the docs. Replace ADD_YOUR_LICENSE_KEY_HERE with your key.

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

class BarcodeScanner extends StatefulWidget {
  @override
  _BarcodeScannerState createState() => _BarcodeScannerState();
}

class _BarcodeScannerState extends State<BarcodeScanner> {
  Barkoder? _barkoder;
  String? scannedResult;
  bool isScanning = false;

  void _onBarkoderViewCreated(Barkoder barkoder) {
    _barkoder = barkoder;

    _barkoder!.setRegionOfInterestVisible(true);
    _barkoder!.setRegionOfInterest(5, 5, 90, 90);
    _barkoder!.setCloseSessionOnResultEnabled(true);
    _barkoder!.setImageResultEnabled(true);
    _barkoder!.setBarcodeThumbnailOnResultEnabled(true);
    _barkoder!.setBeepOnSuccessEnabled(true);
    _barkoder!.setPinchToZoomEnabled(true);
    _barkoder!.setZoomFactor(2.0);

    // Enable the barcode types and add others if you need
    _barkoder!.setBarcodeTypeEnabled(BarcodeType.code128, true);
    _barkoder!.setBarcodeTypeEnabled(BarcodeType.code39, true);
    _barkoder!.setBarcodeTypeEnabled(BarcodeType.ean13, true);
  }

  void _startScanning() {
    setState(() {
      scannedResult = null;
      isScanning = true;
    });

    _barkoder!.startScanning((result) {
      setState(() {
        scannedResult = result.decoderResults[0].textualData;
        isScanning = false;
      });
    });
  }

  void _stopScanning() {
    _barkoder!.stopScanning();
    setState(() {
      isScanning = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Barcode Scanner')),
      body: Column(
        children: [
          Expanded(
            child: BarkoderView(
              licenseKey: 'ADD_YOUR_LICENSE_KEY_HERE',
              onBarkoderViewCreated: _onBarkoderViewCreated,
            ),
          ),
          if (scannedResult != null)
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                children: [
                  Text('Result: $scannedResult'),
                  // Here you can add more details like barcode type, or display the thumbnail
                ],
              ),
            ),
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: isScanning ? null : _startScanning,
                  child: Text('Start Scanning'),
                ),
                ElevatedButton(
                  onPressed: isScanning ? _stopScanning : null,
                  child: Text('Stop Scanning'),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

What the code does:

  • BarkoderView shows the camera preview and takes your licenseKey. Here it fills the space above the buttons.
  • _onBarkoderViewCreated runs when the SDK is ready. It keeps the instance, shows a region of interest covering the central 90% of the view, ends the scan session after the first result, asks for an image and a thumbnail with each result, turns on the success beep, pinch-to-zoom and a 2× zoom, and enables Code 128, Code 39 and EAN-13.
  • _startScanning starts scanning; the result arrives in the callback, where result.decoderResults[0].textualData is the decoded text.
  • _stopScanning stops the camera.

To read other symbologies, add more setBarcodeTypeEnabled lines; the Flutter API reference lists every BarcodeType. To keep scanning after each result, see Going further.

Step 6: Show the widget

Make BarcodeScanner your home screen in lib/main.dart, or add it to your navigation:

import 'package:flutter/material.dart';
import 'barcode_scanner.dart';  // Adjust the path as necessary

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Barcode Scanner',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BarcodeScanner(),
    );
  }
}

Step 7: Run it

Run the app with flutter run, tap Start Scanning and point the camera at a Code 128, Code 39 or EAN-13 barcode.

Going further

Scan many barcodes at once

To keep scanning and collect several codes in one session, use the settings from the Batch MultiScan example in _onBarkoderViewCreated, in place of setCloseSessionOnResultEnabled(true):

_barkoder.setCloseSessionOnResultEnabled(false);
_barkoder.setMulticodeCachingDuration(3000);
_barkoder.setMulticodeCachingEnabled(true);
_barkoder.setMaximumResultsCount(200);

The docs example uses a non-nullable _barkoder; in the widget above, _barkoder is nullable, so write _barkoder!. as the other calls in _onBarkoderViewCreated do. With the session left open, the camera keeps running after each result, so remove isScanning = false; from the result callback and let the Stop Scanning button end the session. More about the mode: Batch MultiScan.

Read passports and ID cards

Enable the ID document type to scan the MRZ (MRZ example):

_barkoder.setBarcodeTypeEnabled(BarcodeType.idDocument, true);

MRZ needs a license key with MRZ enabled. See MRZ scanning.

More modes and settings

The Flutter examples cover continuous scanning, VIN, DPM, DotCode, AR mode, gallery scanning and the South African license parser. For a complete app, start from barKoder's open-source demo apps.

Troubleshooting

  • Android build stops at checkDebugAarMetadata: add the Gradle lines from Step 3.
  • pod install says barkoder_flutter needs a higher minimum iOS version: set platform :ios, '15.0' in ios/Podfile (Step 4).
  • Results start with (Unlicensed) and part of the data is dots: the license key is missing, wrong or expired, or it was made for a different app ID. Check licenseKey, or generate a trial key.
  • The camera doesn't start on iOS: check that NSCameraUsageDescription is in ios/Runner/Info.plist.

Moving from another Flutter scanner?

barKoder has step-by-step guides for replacing Scanbot and Cognex in Flutter apps. The Flutter barcode scanner SDK page covers what the plugin reads, including damaged codes with MatrixSight®, DPM, MRZ and VIN.

Frequently asked questions

How do I add a barcode scanner to a Flutter app?
Why does my Android build stop at checkDebugAarMetadata?
What is the minimum Android version for barkoder_flutter?
Do I need a license key?
Can the Flutter scanner read passports?