Value of optional type ‘UIView?’ must be unwrapped to value type ‘UIView’

Total
0
Shares

Flutter has changed splashScreenView from nonnull to nullable. The declaration has become –

@property(strong, nonatomic, nullable) UIView* splashScreenView;

splashScreenView return nil when no splash screen is set but it was marked nonnull. Which is incorrect. So, in new version of Flutter it has changed to nullable.

Solution

Error Code – Let’s see how splashScreenView can generate error –

var splashScreenView = UIView()
var flutterEngine = FlutterEngine(name: "my flutter engine")
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)

  // compilation error: Value of optional type 'UIView?' must be unwrapped to a value of type 'UIView'
splashScreenView = flutterViewController.splashScreenView

In this code we will get error at line flutterViewController.splashScreenView.

You can see that splashScreenView is stored as UIView() variable. But since flutterViewController.splashScreenView is nullable now, so we need to change splashScreenView variable to optional type using UIView?.

Correct Code

var splashScreenView : UIView? = UIView()

var flutterEngine = FlutterEngine(name: "my flutter engine")
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)

let splashScreenView = flutterViewController.splashScreenView
if let splashScreenView = splashScreenView {
}