ensureInitialized method

WidgetsBinding ensureInitialized ()

Creates and initializes the binding. This function is idempotent; calling it a second time will just return the previously-created instance.

This function will use AutomatedTestWidgetsFlutterBinding if the test was run using flutter test, and LiveTestWidgetsFlutterBinding otherwise (e.g. if it was run using flutter run). (This is determined by looking at the environment variables for a variable called FLUTTER_TEST.)

Implementation

static WidgetsBinding ensureInitialized() {
  if (WidgetsBinding.instance == null) {
    if (Platform.environment.containsKey('FLUTTER_TEST')) {
      AutomatedTestWidgetsFlutterBinding();
    } else {
      LiveTestWidgetsFlutterBinding();
    }
  }
  assert(WidgetsBinding.instance is TestWidgetsFlutterBinding);
  return WidgetsBinding.instance;
}