juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
781 stars 460 forks source link

Hot restart not working on iOS (v3.0.0) #502

Open zunimc opened 1 year ago

zunimc commented 1 year ago

I have this simple code to start. I removed the torchlight and front camera buttons for simplicity.

I am testing it on real devices (iPhone 6S and iPhone 12).

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    MobileScannerController cameraController = MobileScannerController();
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Mobile Scanner'),
        ),
        body: MobileScanner(
          controller: cameraController,
          onDetect: (capture) {
            final List<Barcode> barcodes = capture.barcodes;
            for (final barcode in barcodes) {
              debugPrint('Barcode found! ${barcode.rawValue}');
            }
          },
        ),
      ),
    );
  }
}

Problem is as soon as I hit Hot Restart (Shift+R) camera stops working.

Performing hot restart... Restarted application in 1.097ms. flutter: mobile_scanner: MobileScannerException: code genericError, message: Called start() while already started!

godspeed0310 commented 1 year ago

I'm having the same issue on Android, were you able to find a solution for this?

zunimc commented 1 year ago

I'm having the same issue on Android, were you able to find a solution for this?

Android? That's weird as that OS worked like a charm on my code. Anyway I ended up replacing this library and found a working one: https://pub.dev/packages/code_scan

With very few adjustments I got the same result on both mobile systems. Hope that helps.

godspeed0310 commented 1 year ago

Is this the same code that you used for android too? And yeah it does help, thanks for the library recommendation

zunimc commented 1 year ago

Yes, same code for both iOS and Android.

FutureBuilder<double>(
  future: setScreenHeight(), // this functions returns a custom height for each device.
  builder: (context, snapshot) {
    return SizedBox(
      height: snapshot.data, // result of setScreenHeight()
      child: code_scan.CodeScanner(
        onScan: (code, details, controller) {
          setState(() {
            checkBarcodeFormat(code!);
          });
        },
        overlay: const Center(child: Text('')), // added this to have a clear view on the scan
        formats: const [code_scan.BarcodeFormat.all],
        once: false,
      ),
    );
  },
),