Skip to content

flutter 3.41.4 での実行エラー対応#2

Open
toshi3221 wants to merge 2 commits into
iro-dori:masterfrom
toshi3221:hotfix/fix_for_flutter3.41.4
Open

flutter 3.41.4 での実行エラー対応#2
toshi3221 wants to merge 2 commits into
iro-dori:masterfrom
toshi3221:hotfix/fix_for_flutter3.41.4

Conversation

@toshi3221
Copy link
Copy Markdown

@toshi3221 toshi3221 commented Mar 10, 2026

flutter(3.41.4)アップデートのための修正になります。
以下にてビルド・カメラ表示までを確認しました:

   flutter_barcode_scanner:
     git:
 #      url: git://github.com/iro-dori/flutter_barcode_scanner.git
-      url: https://github.com/iro-dori/flutter_barcode_scanner.git
+      url: https://github.com/toshi3221/flutter_barcode_scanner.git
+      ref: hotfix/fix_for_flutter3.41.4

3.41.4にflutterをアップグレードして実行したらエラーがでて進みません。原因は何ですか?

問題は SwiftFlutterBarcodeScannerPlugin.swift の27行目にあります:

viewController = (UIApplication.shared.delegate?.window??.rootViewController)!

Flutter 3.41.4(iOS側)では UIApplication.shared.delegate?.windownil を返すようになり、末尾の ! による force unwrap でクラッシュしています。

背景

iOS 13以降のScene-based lifecycleでは UIApplication.shared.delegate?.windownil になることがあります。以前のFlutterバージョンでは問題なく動いていたのが、3.41.4でこの挙動が変わった可能性があります。

修正内容は次のとおりです:

// 旧
viewController = (UIApplication.shared.delegate?.window??.rootViewController)!

// 新(iOS 13+ 対応)
if let window = UIApplication.shared.connectedScenes
    .compactMap({ $0 as? UIWindowScene })
    .flatMap({ $0.windows })
    .first(where: { $0.isKeyWindow }),
   let vc = window.rootViewController {
    viewController = vc
} else {
    viewController = (UIApplication.shared.delegate?.window??.rootViewController) ?? UIViewController()
}

以下のエラーがでてカメラを起動出来ません。

flutter: [C.BROWSER][AboutBlankPage]: load camera
Attempt to present <flutter_barcode_scanner.BarcodeScannerViewController: 0x10c01fa00> on <UIViewController: 0x106a2bf00> (from <UIViewController: 0x106a2bf00>) whose view is not in the window hierarchy.

エラーを見ると、WebView内からバーコードスキャナーを起動しようとしており、iOSのViewControllerヒエラルキーに問題が発生しています。コードを確認します。

原因が分かりました。プラグインの SwiftFlutterBarcodeScannerPlugin.swift でアプリ起動時に viewController を一度だけキャプチャしています(register(with:) の中)。しかし、バーコードスキャナーを実際に呼び出すときにはウィンドウヒエラルキーが変わっている可能性があるため、その古い viewController は「not in the window hierarchy」になります。

修正方法: handle 呼び出し時に動的に最上位のViewControllerを取得するように変更する必要があります。 0bb0c89

関連Issue

iOS 26 にて実機デバッグが出来ない(Flutterアップグレード) · Issue #90 · iro-dori/ploomx-app

3.41.4にflutterをアップグレードして実行したらエラーがでて進みません。原因は何ですか?

原因が特定できました。

問題は SwiftFlutterBarcodeScannerPlugin.swift の27行目にあります:

viewController = (UIApplication.shared.delegate?.window??.rootViewController)!

Flutter 3.41.4(iOS側)では UIApplication.shared.delegate?.window が nil を返すようになり、末尾の ! による force unwrap でクラッシュしています。

背景
iOS 13以降のScene-based lifecycleでは UIApplication.shared.delegate?.window が nil になることがあります。以前のFlutterバージョンでは問題なく動いていたのが、3.41.4でこの挙動が変わった可能性があります。

修正方法
~/.pub-cache/git/flutter_barcode_scanner-ee146679.../ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift の27行目を修正できますが、pub-cacheの直接編集は推奨されません。

フォークしたリポジトリで修正するか、pubspec.yaml の git 参照にパッチを当てるのがベストです。修正内容は次のとおりです:

// 旧
viewController = (UIApplication.shared.delegate?.window??.rootViewController)!

// 新(iOS 13+ 対応)
if let window = UIApplication.shared.connectedScenes
    .compactMap({ $0 as? UIWindowScene })
    .flatMap({ $0.windows })
    .first(where: { $0.isKeyWindow }),
   let vc = window.rootViewController {
    viewController = vc
} else {
    viewController = (UIApplication.shared.delegate?.window??.rootViewController) ?? UIViewController()
}
@toshi3221
Copy link
Copy Markdown
Author

@devkato
macOS Sequoia 15.7.5、Xcode 26.2にて以下デバイスにてコネブラのQR読込を確認しましたのでマージをお願いしますm_ _m

  • iPhone XR 17.5.1
  • iPhone 13 mini 26.3.1

Attempt to present <flutter_barcode_scanner.BarcodeScannerViewController: 0x10c01fa00> on <UIViewController: 0x106a2bf00> (from <UIViewController: 0x106a2bf00>) whose view is not in the window hierarchy.

とエラーがでてカメラを起動出来ません。

エラーを見ると、WebView内からバーコードスキャナーを起動しようとしており、iOSのViewControllerヒエラルキーに問題が発生しています。コードを確認します。

原因が分かりました。プラグインの SwiftFlutterBarcodeScannerPlugin.swift でアプリ起動時に viewController を一度だけキャプチャしています(register(with:) の中)。しかし、バーコードスキャナーを実際に呼び出すときにはウィンドウヒエラルキーが変わっている可能性があるため、その古い viewController は「not in the window hierarchy」になります。

修正方法: handle 呼び出し時に動的に最上位のViewControllerを取得するように変更する必要があります。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant