ensureInitialized static method

WidgetsBinding ensureInitialized()
override

Returns an instance of the binding that implements WidgetsBinding. If no binding has yet been initialized, the WidgetsFlutterBinding class is used to create and initialize one.

You only need to call this method if you need the binding to be initialized before calling runApp.

In the flutter_test framework, testWidgets initializes the binding instance to a TestWidgetsFlutterBinding, not a WidgetsFlutterBinding. See TestWidgetsFlutterBinding.ensureInitialized.

Implementation

static WidgetsBinding ensureInitialized() {
  if (!_initFlag) {
    MockBinding();
    _initFlag = true;
  }
  // if (WidgetsBinding.instance == null) MockBinding();
  return WidgetsBinding.instance;
}