initServiceExtensions method

  1. @protected
  2. @mustCallSuper
void initServiceExtensions()

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).

The following code registers a service extension that is only included in debug builds.
link
void myRegistrationFunction() {
  assert(() {
    // Register your service extension here.
    return true;
  }());
}

A service extension registered with the following code snippet is available in debug and profile mode.
link
void myOtherRegistrationFunction() {
  // kReleaseMode is defined in the 'flutter/foundation.dart' package.
  if (!kReleaseMode) {
    // Register your service extension here.
  }
}

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: FoundationServiceExtensions.reassemble.name,
      callback: reassembleApplication,
    );
    return true;
  }());

  if (!kReleaseMode) {
    if (!kIsWeb) {
      registerSignalServiceExtension(
        name: FoundationServiceExtensions.exit.name,
        callback: _exitApplication,
      );
    }
    // These service extensions are used in profile mode applications.
    registerStringServiceExtension(
      name: FoundationServiceExtensions.connectedVmServiceUri.name,
      getter: () async => connectedVmServiceUri ?? '',
      setter: (String uri) async {
        connectedVmServiceUri = uri;
      },
    );
    registerStringServiceExtension(
      name: FoundationServiceExtensions.activeDevToolsServerAddress.name,
      getter: () async => activeDevToolsServerAddress ?? '',
      setter: (String serverAddress) async {
        activeDevToolsServerAddress = serverAddress;
      },
    );
  }

  assert(() {
    registerServiceExtension(
      name: FoundationServiceExtensions.platformOverride.name,
      callback: (Map<String, String> parameters) async {
        if (parameters.containsKey('value')) {
          final String value = parameters['value']!;
          debugDefaultTargetPlatformOverride = null;
          for (final TargetPlatform candidate in TargetPlatform.values) {
            if (candidate.name == value) {
              debugDefaultTargetPlatformOverride = candidate;
              break;
            }
          }
          _postExtensionStateChangedEvent(
            FoundationServiceExtensions.platformOverride.name,
            defaultTargetPlatform.name,
          );
          await reassembleApplication();
        }
        return <String, dynamic>{
          'value': defaultTargetPlatform.name,
        };
      },
    );

    registerServiceExtension(
      name: FoundationServiceExtensions.brightnessOverride.name,
      callback: (Map<String, String> parameters) async {
        if (parameters.containsKey('value')) {
          debugBrightnessOverride = switch (parameters['value']) {
            'Brightness.light' => ui.Brightness.light,
            'Brightness.dark'  => ui.Brightness.dark,
            _ => null,
          };
          _postExtensionStateChangedEvent(
            FoundationServiceExtensions.brightnessOverride.name,
            (debugBrightnessOverride ?? platformDispatcher.platformBrightness).toString(),
          );
          await reassembleApplication();
        }
        return <String, dynamic>{
          'value': (debugBrightnessOverride ?? platformDispatcher.platformBrightness).toString(),
        };
      },
    );
    return true;
  }());
  assert(() {
    _debugServiceExtensionsRegistered = true;
    return true;
  }());
}