mobile_scanner icon indicating copy to clipboard operation
mobile_scanner copied to clipboard

Getting randomly: Called start() while already started!

Open baptistesx opened this issue 2 years ago • 6 comments

Hi everyone,

i've implemented the scanner in a Stack widget and customized the onDetect callback. When arriving on the page containing the Scanner, i'm getting very often: flutter: MobileScanner: Called start() while already started! (and black screen (with actions icons) instead of camera)

I tried to simply copy past the example file: barcode_scanner_controller.dart but same thing...

I don't know what to do, if you have a recommendation?

baptistesx avatar Sep 23 '22 15:09 baptistesx

Same here. Followed the sample as well. Getting this error all the time. Very annoying. (On iOS 15.6.1)

MagnusJohansson avatar Sep 25 '22 16:09 MagnusJohansson

I've released v3.0.0-beta.1 which has many fixes. Can you please try with that version and if the issue still exists, comment on this issue? thanks in advance.

juliansteenbakker avatar Sep 28 '22 18:09 juliansteenbakker

I am getting the same issue on v1.1.1 and 2.0.0 - but only on iOS as far as I can tell. On Android everything seems to be fine.

Tried to test with 3.0.0-beta.1 but this fails on build: "Could not build the precompiled application for the device" -> mobile_scanner-3.0.0-beta.1/lib/src/mobile_scanner.dart:74:3: Error: 'Uint8List' isn't a type.

The build problem goes away when using v.2.0.0 (but then of course the original error kicks in again)

Would be happy to test on 3.0.0-beta.1 if someone could point me in the right direction to fix the build problem.

peukerjonathan avatar Oct 10 '22 15:10 peukerjonathan

I managed to build with 3.0.0-beta.1 (flutter upgrade did the trick)

The error is now reverted.

Instead of getting flutter: MobileScanner: Called start() while already started! (along with the described black screen) I am getting flutter: MobileScanner: Called stop() while already stopped! Along with that the camera vanishes and I can see the underlying widget.

This is happening only on my iOS build (Tested on Apple iPhone 12 & 13) - so far I haven't noticed any issues on my Android builds. If I can provide any information helpful in resolving this issue please let me know - I will be more than happy to assist.

peukerjonathan avatar Oct 11 '22 07:10 peukerjonathan

Hello there! Any thoughts on this? I would be more than happy to help to resolve this issue. Unfortunately at the moment I have no idea how to do so. In case I have some time this week I will attempt to debug myself and provide any information on the issue I can find.

peukerjonathan avatar Oct 17 '22 15:10 peukerjonathan

Hello! I am also having same issue only in iOS even with v3.0.0-beta.1, after first scan I get a black screen and the error in console: flutter: MobileScanner: Called stop() while already stopped!

anthonycuervo23 avatar Oct 21 '22 15:10 anthonycuervo23

As far as I found out now the message flutter: MobileScanner: Called stop() while already stopped! originates from the file mobile_scanner-3.0.0-beta.1/ios/Classes/SwiftMobileScannerPlugin.swift

This messages is triggered when the stop method is called and the device property is already nil.

My Swift knowledge is de facto non-existant, but I am still trying to figure out what is going on here.

As far as it comes to the code in this class this property seems initialized as nil and then set to a AVCaptureDevice in the start method. The only way to reset it to nil in this class is the stop method. And the message appears when the stop method is called and the device is already nil

So from what I understand there are two possible things going on here:

  1. Something randomly triggers the stop method.
  2. Something else is "overriding" the device property to nil causing unexpected behaviour.

Unfortunately I am at a total loss on how to investigate further. If someone can tell me how to debug this I will gladly do so and post the results (logs whatever) here.

peukerjonathan avatar Nov 03 '22 09:11 peukerjonathan

I am still working on a new version which does not have this problem anymore. The code can be found on the master branch but there are still a few issues i have to work out.

juliansteenbakker avatar Nov 03 '22 09:11 juliansteenbakker

That sounds amazing, thanks for the feedback!

peukerjonathan avatar Nov 03 '22 10:11 peukerjonathan

Please checkout v3.0.0-beta.2 which solves this issue.

juliansteenbakker avatar Nov 18 '22 09:11 juliansteenbakker

Unfortunately I have to disagree. :( I just updated to the new version (3.0.0-beta.2) and tested on my app using an iPhone 13. The problem unfortunately was still there.

However with the new version I was able to determine the source causing the problem in my case (which then led to another problem, but more on that later)

In my case the problem is caused by this implementation: I have a stateful widget - in the build method of that widget this is implemented:

Widget build(BuildContext context) {

    ...

    // The widget has two states:

    // _scannerFlag = true -> render MobileScanner. As soon as a code was scanned this to set to false

    // _scannerFlag = false -> show result of recent scan. Use a button to set to true -> open scanner to do another scan

    if(_scannerFlag) {

        ...

        MobileScannerController cameraController = MobileScannerController();

        ...

        return new WillPopScope(

            ...

            body: MobileScanner(

                ...

                controller: cameraController,

                ...

            ),

            ...

        );

        ...

    ...

    } else {

        // render someting else

    }

    ...

}

Then in the background there is a process running which is syncing data with a Webserver. If data on the webserver was changed a call to setState(() {...}); is triggered causing the whole widget (including the scanner) to re-render. So MobileScannerController cameraController = MobileScannerController(); is called again while it is already present and running. This is when the problem occurs and I get Called stop() when already stopped (Scanner switches to black screen)

I was able to circumvent this by pulling MobileScannerController cameraController = MobileScannerController(); from the build method so there is no new instance of MobileScannerController created when the widget re-renders.

However this leads to another problem: In this case I can scan exactly one QR Code. (_scannerFlag is set to false and something else is rendered) After I reopen the scanner (set _scannerFlag to true) the scanner opens fine but it does never fire onDetect again.

Any thoughts on that?

peukerjonathan avatar Nov 30 '22 12:11 peukerjonathan

This problem still exists using 3.0.0-beta.2, my code is very simple (no state involved):

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

  @override
  Widget build(BuildContext context) {
    return MobileScanner(
      onDetect: (capture) {
        final List<Barcode> barcodes = capture.barcodes;
        String? barcodeValue;
        for (final barcode in barcodes) {
          barcodeValue = barcode.rawValue;
          break;
        }
        Navigator.of(context).pop(barcodeValue);
      },
    );
  }
}

And on iOS 16 I get this error:

flutter: mobile_scanner: MobileScannerException: code genericError, message: Called start() while already started!

@juliansteenbakker any idea why this is still happening?

Note I have reproduced this with 3.0.0, 3.0.0-beta.1 and 3.0.0-beta.2 - seems to always happen right after giving permission to use the camera (first time) - afterwards the bug does not appear to happen.

tmpfs avatar Mar 13 '23 06:03 tmpfs