ensureInitialized method Null safety

WidgetsBinding ensureInitialized()

Returns an instance of the WidgetsBinding, creating and initializing it if necessary. If one is created, it will be a WidgetsFlutterBinding. If one was previously initialized, then it will at least implement WidgetsBinding.

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.


static WidgetsBinding ensureInitialized() {
  if (WidgetsBinding.instance == null)
  return WidgetsBinding.instance!;