flutter 3.41.4 での実行エラー対応#2
Open
toshi3221 wants to merge 2 commits into
Open
Conversation
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()
}
Author
|
@devkato
|
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を取得するように変更する必要があります。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.43.41.4にflutterをアップグレードして実行したらエラーがでて進みません。原因は何ですか?
問題は
SwiftFlutterBarcodeScannerPlugin.swiftの27行目にあります: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でこの挙動が変わった可能性があります。修正内容は次のとおりです:
以下のエラーがでてカメラを起動出来ません。
エラーを見ると、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