initInstances method
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
link
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;
assert(() {
_debugAddStackFilters();
return true;
}());
// Initialization of [_buildOwner] has to be done after
// [super.initInstances] is called, as it requires [ServicesBinding] to
// properly setup the [defaultBinaryMessenger] instance.
_buildOwner = BuildOwner();
buildOwner!.onBuildScheduled = _handleBuildScheduled;
platformDispatcher.onLocaleChanged = handleLocaleChanged;
SystemChannels.navigation.setMethodCallHandler(_handleNavigationInvocation);
SystemChannels.backGesture.setMethodCallHandler(
_handleBackGestureInvocation,
);
assert(() {
FlutterErrorDetails.propertiesTransformers.add(debugTransformDebugCreator);
return true;
}());
platformMenuDelegate = DefaultPlatformMenuDelegate();
}