window top-level property Null safety

SingletonFlutterWindow window
final

The SingletonFlutterWindow representing the main window for applications where there is only one window, such as applications designed for single-display mobile devices.

Applications that are designed to use more than one window should interact with the WidgetsBinding.instance.platformDispatcher instead.

Consider avoiding static references to this singleton through PlatformDispatcher.instance and instead prefer using a binding for dependency resolution such as WidgetsBinding.instance.window.

Static access of this window object means that Flutter has few, if any options to fake or mock the given object in tests. Even in cases where Dart offers special language constructs to forcefully shadow such properties, those mechanisms would only be reasonable for tests and they would not be reasonable for a future of Flutter where we legitimately want to select an appropriate implementation at runtime.

The only place that WidgetsBinding.instance.window is inappropriate is if access to these APIs is required before the binding is initialized by invoking runApp() or WidgetsFlutterBinding.instance.ensureInitialized(). In that case, it is necessary (though unfortunate) to use the PlatformDispatcher.instance object statically.

See also:

  • PlatformDispatcher.views, contains the current list of Flutter windows belonging to the application, including top level application windows like this one.

Implementation

final SingletonFlutterWindow window = SingletonFlutterWindow._(0, PlatformDispatcher.instance);