initServiceExtensions method
- @protected
- @mustCallSuper
Called when the binding is initialized, to register service extensions.
Bindings that want to expose service extensions should overload this method to register them using calls to registerSignalServiceExtension, registerBoolServiceExtension, registerNumericServiceExtension, and registerServiceExtension (in increasing order of complexity).
Implementations of this method must call their superclass implementation.
A registered service extension can only be activated if the vm-service is included in the build, which only happens in debug and profile mode. Although a service extension cannot be used in release mode its code may still be included in the Dart snapshot and blow up binary size if it is not wrapped in a guard that allows the tree shaker to remove it (see sample code below).
Sample Code
The following code registers a service extension that is only included in debug builds:
assert(() {
// Register your service extension here.
return true;
}());
A service extension registered with the following code snippet is available in debug and profile mode:
if (!const bool.fromEnvironment('dart.vm.product')) {
Both guards ensure that Dart's tree shaker can remove the code for the service extension in release builds.
See also:
Implementation
@protected
@mustCallSuper
void initServiceExtensions() {
assert(!_debugServiceExtensionsRegistered);
assert(() {
registerSignalServiceExtension(
name: 'reassemble',
callback: reassembleApplication,
);
return true;
}());
const bool isReleaseMode = bool.fromEnvironment('dart.vm.product');
if (!isReleaseMode) {
registerSignalServiceExtension(
name: 'exit',
callback: _exitApplication,
);
}
assert(() {
registerServiceExtension(
name: 'platformOverride',
callback: (Map<String, String> parameters) async {
if (parameters.containsKey('value')) {
switch (parameters['value']) {
case 'android':
debugDefaultTargetPlatformOverride = TargetPlatform.android;
break;
case 'iOS':
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
break;
case 'fuchsia':
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
break;
case 'default':
default:
debugDefaultTargetPlatformOverride = null;
}
await reassembleApplication();
}
return <String, dynamic>{
'value': defaultTargetPlatform
.toString()
.substring('$TargetPlatform.'.length),
};
}
);
return true;
}());
assert(() { _debugServiceExtensionsRegistered = true; return true; }());
}