Flutter: No Windows have a root view controller, cannot save application state
When encountering the error message “No Windows have a root view controller, cannot save application state” in Flutter, it typically means that there is an issue with the configuration or setup of your project.
To better understand this error, let’s go through some possible reasons and solutions:
- Missing or incorrect root view controller: This error often occurs when the root view controller is not properly set in the AppDelegate (iOS) or MainActivity (Android) files.
- Missing or incorrect Flutter engine initialization: Another possible cause is the absence of proper Flutter engine initialization in your code.
- Improper project structure or dependency issues: Sometimes, the error may be caused by a mismatch between Flutter and Dart versions or improper project structure.
Solution: Double-check the configuration in your platform-specific code and ensure that the root view controller is set correctly. Here are some examples of how it should be set:
iOS (AppDelegate.swift):
// Inside `didFinishLaunchingWithOptions` method
GeneratedPluginRegistrant.register(with: self)
// Set root view controller
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = FlutterViewController()
self.window?.makeKeyAndVisible()
Android (MainActivity.kt):
// Inside `onCreate` method
GeneratedPluginRegistrant.registerWith(this)
// Set root view controller
val flutterView = FlutterMain.createView(this, lifecycle, "route")
setContentView(flutterView)
Solution: Make sure the Flutter engine is initialized before trying to set the root view controller. Here’s an example:
iOS (AppDelegate.swift):
// Inside `didFinishLaunchingWithOptions` method
FlutterEngineCache.getInstance().put("my_engine_id", MyFlutterEngine()) // Replace with your own engine id and implementation
// Set root view controller
self.window = UIWindow(frame: UIScreen.main.bounds)
let flutterViewController = FlutterViewController(engine: FlutterEngineCache.sharedInstance().engine(withName: "my_engine_id"), nibName: nil, bundle: nil)
self.window?.rootViewController = flutterViewController
self.window?.makeKeyAndVisible()
Android (MainActivity.kt):
// Inside `onCreate` method
val flutterEngine = FlutterEngine(this)
flutterEngine.dartExecutor.executeDartEntrypoint(FlutterMain.findAppBundlePath())
FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine) // Replace with your own engine id
// Set root view controller
val flutterView = FlutterMain.createView(this, lifecycle, "route")
flutterView.attachToFlutterEngine(FlutterEngineCache.getInstance().engine("my_engine_id"))
setContentView(flutterView)
Solution: Verify that your Flutter and Dart versions are compatible, and ensure that your project structure is correct. Try running “flutter clean” to clear any cached artifacts and rebuild your project to fix any dependency issues.
By following these troubleshooting steps and ensuring proper configuration, you should be able to resolve the “No Windows have a root view controller, cannot save application state” error in your Flutter project.