runApp function

void runApp(
  1. Widget app
)

Inflate the given widget and attach it to the screen.

The widget is given constraints during layout that force it to fill the entire screen. If you wish to align your widget to one side of the screen (e.g., the top), consider using the Align widget. If you wish to center your widget, you can also use the Center widget.

Calling runApp again will detach the previous root widget from the screen and attach the given widget in its place. The new widget tree is compared against the previous widget tree and any differences are applied to the underlying render tree, similar to what happens when a StatefulWidget rebuilds after calling State.setState.

Initializes the binding using WidgetsFlutterBinding if necessary.

Application shutdown

This widget tree is not torn down when the application shuts down, because there is no way to predict when that will happen. For example, a user could physically remove power from their device, or the application could crash unexpectedly, or the malware on the device could forcibly terminate the process.

Applications are responsible for ensuring that they are well-behaved even in the face of a rapid unscheduled termination.

To artificially cause the entire widget tree to be disposed, consider calling runApp with a widget such as SizedBox.shrink.

To listen for platform shutdown messages (and other lifecycle changes), consider the AppLifecycleListener API.

Dismissing Flutter UI via platform native methods

An application may have both Flutter and non-Flutter UI in it. If the application calls non-Flutter methods to remove Flutter based UI such as platform native API to manipulate the platform native navigation stack, the framework does not know if the developer intends to eagerly free resources or not. The widget tree remains mounted and ready to render as soon as it is displayed again.

To release resources more eagerly, establish a platform channel and use it to call runApp with a widget such as SizedBox.shrink when the framework should dispose of the active widget tree.

See also:

Implementation

void runApp(Widget app) {
  final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
  assert(binding.debugCheckZone('runApp'));
  binding
    ..scheduleAttachRootWidget(binding.wrapWithDefaultView(app))
    ..scheduleWarmUpFrame();
}