ensureInitialized method Null safety

TestWidgetsFlutterBinding ensureInitialized(
  1. [@visibleForTesting Map<String, String>? environment]
)

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.

If FLUTTER_TEST is set with a value of 'true', then this test was invoked by flutter test. If FLUTTER_TEST is not set, or if it is set to 'false', then this test was invoked by flutter run.

Browser environments do not currently support the LiveTestWidgetsFlutterBinding, so this function will always set up an AutomatedTestWidgetsFlutterBinding when run in a web browser.

The parameter environment is used to test the test framework itself by checking how it reacts to different environment variable values, and should not be used outside of this context.

If a TestWidgetsFlutterBinding subclass was explicitly initialized before calling ensureInitialized, then that version of the binding is returned regardless of the logic described above. This allows tests to force a specific test binding to be used.

This is called automatically by testWidgets.

Implementation

static TestWidgetsFlutterBinding ensureInitialized([@visibleForTesting Map<String, String>? environment]) {
  if (_instance != null)
    return _instance!;
  return binding.ensureInitialized(environment);
}