iosfluttermobileavfoundationbarcode

Flutter mobile_scanner not detecting QR codes on iOS devices


this is my first Stack Overflow post! I am intermediate at building Flutter apps and now I am trying to use the mobile_scanner 4.0.1 package in Flutter as it offers a nice customizeable fullscreen scanwindow. Detecting barcodes works as intended on Android devices however on iOS devices, the _mobileScannerController still works (i.e. I can turn the camera around, restart the controller, turn on flashlight etc.), but it never detects a barcode and calls onDetect. My iOS devices have all had their camera app QR options turned on in settings and I get no error message what so ever. The tested devices has ranged in age from 1 year old to 7 years old.

I am 100% certain I have followed the instructions on how to install mobile_scanner correctly, I´ve tried older versions, old and new iOS devices, turning QR option in camera on and off (iPhone settings -> general -> camera...) and am afraid the problem lies outside my code and in my installation somwhere. I have asked for NSCameraUsageDescription in the info.plist file. This is my code:

import 'dart:developer';


import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

import 'scanner_error_widget.dart';

class BarcodeScannerWithScanWindow extends StatefulWidget {

  late _BarcodeScannerWithScanWindowState barcodeScannerState;
  @override
  _BarcodeScannerWithScanWindowState createState() {
    barcodeScannerState = _BarcodeScannerWithScanWindowState();
    return barcodeScannerState;
  }


  getState() => barcodeScannerState;
}

class _BarcodeScannerWithScanWindowState
    extends State<BarcodeScannerWithScanWindow> {

  refresh() {
    log("qr_scanner: Refreshing _mobileScannerController");
    _mobileScannerController.stop();
    _mobileScannerController.start();
  }

  final MobileScannerController _mobileScannerController = MobileScannerController(
    formats: [BarcodeFormat.qrCode],
    facing: CameraFacing.back,
    detectionSpeed: DetectionSpeed.noDuplicates,
    detectionTimeoutMs: 60*1000
  );


  Barcode? barcode;
  BarcodeCapture? capture;

  @override
  void initState() {
    log("INIT");
    super.initState();
    _mobileScannerController.start();
  }

  @override
  dispose() {
    log("DISPOSE");
    _mobileScannerController.dispose();
    super.dispose();
  }

  Future<void> onDetect(BarcodeCapture barcode) async {
    log("DETECTED!");
    capture = barcode;
    setState(() => this.barcode = barcode.barcodes.first);
  }

  MobileScannerArguments? arguments;

  @override
  Widget build(BuildContext context) {
    final scanWindow = Rect.fromCenter(
      center: MediaQuery.of(context).size.center(const Offset(0, -100)),//.center(Offset.zero),
      width: 200,
      height: 200,
    );


    return Scaffold(

      appBar: AppBar(
        elevation: 8.0,
        backgroundColor: Colors.black,
        automaticallyImplyLeading: false,


        // filters
        actions: [
          IconButton(
            onPressed: () {
              _showInfoDialog(context);
            },
            icon: Icon(Icons.info_outlined, color: Colors.white),
          ),
          const SizedBox(width: 8.0),
        ],
      ),


      body: Builder(
        builder: (context) {
          return Stack(
            fit: StackFit.expand,
            children: [
              MobileScanner(
                fit: BoxFit.cover,
                scanWindow: scanWindow,
                controller: _mobileScannerController,
                onScannerStarted: (arguments) {
                  print("SCANNER STARTED");
                  setState(() {
                    this.arguments = arguments;
                  });
                },
                errorBuilder: (context, error, child) {
                  return ScannerErrorWidget(error: error);
                },
                onDetect: onDetect,
              ),

              CustomPaint(
                painter: ScannerOverlay(scanWindow),
              ),
              

            ],
          );
        },
      ),
    );
  }
}

Thank you for taking your time!


Solution

  • maybe check issue on github developper page : it seems that you are not alone https://github.com/juliansteenbakker/mobile_scanner/issues/967 https://github.com/juliansteenbakker/mobile_scanner/issues/1017

    by the way you can maybe try to use the flutter plugin using mlkit from google which is working well in case you are not able to debug/see any logs for those one https://pub.dev/packages/google_mlkit_barcode_scanning