initInstances method Null safety

  1. @override
void initInstances()
override

The initialization method. Subclasses override this method to hook into the platform and otherwise configure their services. Subclasses must call "super.initInstances()".

The binding is not fully initialized when this method runs (for example, other binding mixins may not yet have run their initInstances method). For this reason, code in this method should avoid invoking callbacks or synchronously triggering any code that would normally assume that the bindings are ready.

By convention, if the service is to be provided as a singleton, it should be exposed as MixinClassName.instance, a static getter with a non-nullable return type that returns MixinClassName._instance, a static field that is set by initInstances(). To improve the developer experience, the return value should actually be BindingBase.checkInstance(_instance) (see checkInstance), as in the example below.
mixin BazBinding on BindingBase {
  @override
  void initInstances() {
    super.initInstances();
    _instance = this;
    // ...binding initialization...
  }

  static BazBinding get instance => BindingBase.checkInstance(_instance);
  static BazBinding? _instance;

  // ...binding features...
}

Implementation

@override
void initInstances() {
  super.initInstances();
  _instance = this;
  _defaultBinaryMessenger = createBinaryMessenger();
  _restorationManager = createRestorationManager();
  _initKeyboard();
  initLicenses();
  SystemChannels.system.setMessageHandler((dynamic message) => handleSystemMessage(message as Object));
  SystemChannels.lifecycle.setMessageHandler(_handleLifecycleMessage);
  SystemChannels.platform.setMethodCallHandler(_handlePlatformMessage);
  readInitialLifecycleStateFromNativeWindow();
}