fix: prevent iOS crash when rootViewController is nil during plugin registration#127
Open
Barba2k2 wants to merge 3 commits intoCodingWithTashi:mainfrom
Open
fix: prevent iOS crash when rootViewController is nil during plugin registration#127Barba2k2 wants to merge 3 commits intoCodingWithTashi:mainfrom
Barba2k2 wants to merge 3 commits intoCodingWithTashi:mainfrom
Conversation
ea5d282 to
5aee169
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical iOS crash caused by force-unwrapping a nil rootViewController during plugin registration. The fix changes the viewController from a static stored property to a computed property that safely retrieves the root view controller when needed, with support for modern iOS using UIWindowScene and a fallback for older versions.
Changes:
- Converted
viewControllerfrom a stored property to a computed optional property that lazily retrieves the root view controller - Removed the force-unwrapped assignment in the
registermethod that was causing the crash - Added safe unwrapping with guard statements at all usage sites of
viewController
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift | Changed viewController to a computed optional property with safe retrieval logic, removed crash-prone force unwrap in register method, and added guard statements at all usage points |
| example/pubspec.lock | Updated transitive dependencies (meta and test_api) - routine dependency updates |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
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.



Summary
rootViewControllerwas force-unwrapped during plugin registration when it was stillnilviewControllerfrom a static stored property to a computed property that safely retrieves the root view controllerUIWindowScenefor modern iOS with fallback for older versionsProblem
The app crashed with "force unwrapped a nil value" at line 64 of
SwiftFlutterBarcodeScannerPlugin.swiftduring plugin registration becauserootViewControlleris not yet available at that point in the app lifecycle.Solution
Changed the static
viewControllerproperty to a computed property that lazily and safely fetches the root view controller when actually needed (not during registration).