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
745 stars 444 forks source link

IOS emulator not work #1019

Closed maxmeng93 closed 1 month ago

maxmeng93 commented 1 month ago

I used the official demo provided, but it does not work on the iOS simulator, it shows a black screen.

import 'package:mobile_scanner/mobile_scanner.dart';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Mobile Scanner')),
      body: MobileScanner(
        // fit: BoxFit.contain,
        onDetect: (capture) {
          final List<Barcode> barcodes = capture.barcodes;
          final Uint8List? image = capture.image;
          for (final barcode in barcodes) {
            debugPrint('Barcode found! ${barcode.rawValue}');
          }
        },
      ),
    );
  }

demo_scan

[✓] Flutter (Channel stable, 3.19.6, on macOS 14.0 23A344 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.19.6 on channel stable at /Users/yogo/project/app/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (13 hours ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/yogo/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_SDK_ROOT = /Users/yogo/Library/Android/sdk
    • Java binary at: /Users/yogo/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/232.10300.40.2321.11567975/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Users/yogo/Downloads/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Users/yogo/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/232.10300.40.2321.11567975/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] Android Studio (version 2023.2)
    • Android Studio at /Users/yogo/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/232.10300.40.2321.11668458/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.88.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.86.0

[✓] Proxy Configuration
    • HTTP_PROXY is set
    • NO_PROXY is localhost,127.0.0.1,::1
    • NO_PROXY contains localhost
    • NO_PROXY contains 127.0.0.1
    • NO_PROXY contains ::1

[✓] Connected device (4 available)
    • xxxxxxxiPhone (mobile)        • 00008110-000E602C3E44401E            • ios            • iOS 17.4.1 21E236
    • iPhone 15 Pro Max (mobile) • 5A15D2C8-08DE-4033-891C-CC7E3800782A • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)            • macos                                • darwin-arm64   • macOS 14.0 23A344 darwin-arm64
    • Chrome (web)               • chrome                               • web-javascript • Google Chrome 124.0.6367.60

[✓] Network resources
    • All expected network resources are available.

• No issues found!
Process finished with exit code 0
navaronbracke commented 1 month ago

You have to test using a real device, this is working as intended.

The iOS Simulator does not provide a "real" camera to provide images with, so mobile_scanner would never work on it.

The Android Emulator does provide a "fake" camera view, but unless that fake view provides an image with a barcode, you can't test using that either.

maxmeng93 commented 1 month ago

@navaronbracke I tested the example barcode_scanner_controller.dart in mobile_scanner@5.0.0-beta.3 on my iPhone. When I first entered the page, it showed a black screen without rendering the camera view on the page, but it could scan correctly. However, when I clicked the start button at this time, the page would throw an error.

https://github.com/juliansteenbakker/mobile_scanner/assets/16930628/85c7c65b-5a26-4777-8a4f-18b196333a5c

maxmeng93 commented 1 month ago

When I commented out unawaited(controller.start()); in initState(), and then clicked the start button, everything became normal.

navaronbracke commented 4 weeks ago

That seems to be an oversight in the sample indeed. Since we have a manual "start" button, starting the controller in initState() defeats the purpose of said sample. I wonder why it only causes an issue on iOS, though? (I have been using that exact sample to test things out on Android / MacOS and web; but I didn't have an iOS device to test things on for a while)

I'll fix up the sample and test with an iOS device.