fix: prevent iOS crash when rootViewController is nil during plugin registration#127
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 pr needs to merged asap. |
|
@CodingWithTashi review and merge please |
CodingWithTashi
left a comment
There was a problem hiding this comment.
Thank you for your contribution



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).