initServiceExtensions method

  1. @override
void initServiceExtensions ()
override

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

{@inject-html}

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

{@inject-html}

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

Both guards ensure that Dart's tree shaker can remove the code for the service extension in release builds.

See also:

Implementation

@override
void initServiceExtensions() {
  super.initServiceExtensions();

  if (!kReleaseMode) {
    registerSignalServiceExtension(
      name: 'debugDumpApp',
      callback: () {
        debugDumpApp();
        return debugPrintDone;
      },
    );

    if (!kIsWeb) {
      registerBoolServiceExtension(
        name: 'showPerformanceOverlay',
        getter: () =>
        Future<bool>.value(WidgetsApp.showPerformanceOverlayOverride),
        setter: (bool value) {
          if (WidgetsApp.showPerformanceOverlayOverride == value)
            return Future<void>.value();
          WidgetsApp.showPerformanceOverlayOverride = value;
          return _forceRebuild();
        },
      );
    }

    registerServiceExtension(
      name: 'didSendFirstFrameEvent',
      callback: (_) async {
        return <String, dynamic>{
          // This is defined to return a STRING, not a boolean.
          // Devtools, the Intellij plugin, and the flutter tool all depend
          // on it returning a string and not a boolean.
          'enabled': _needToReportFirstFrame ? 'false' : 'true',
        };
      },
    );

    // This returns 'true' when the first frame is rasterized, and the trace
    // event 'Rasterized first useful frame' is sent out.
    registerServiceExtension(
      name: 'didSendFirstFrameRasterizedEvent',
      callback: (_) async {
        return <String, dynamic>{
          // This is defined to return a STRING, not a boolean.
          // Devtools, the Intellij plugin, and the flutter tool all depend
          // on it returning a string and not a boolean.
          'enabled': firstFrameRasterized ? 'true' : 'false',
        };
      },
    );

    // Expose the ability to send Widget rebuilds as [Timeline] events.
    registerBoolServiceExtension(
      name: 'profileWidgetBuilds',
      getter: () async => debugProfileBuildsEnabled,
      setter: (bool value) async {
        if (debugProfileBuildsEnabled != value)
          debugProfileBuildsEnabled = value;
      },
    );
  }

  assert(() {
    registerBoolServiceExtension(
      name: 'debugAllowBanner',
      getter: () => Future<bool>.value(WidgetsApp.debugAllowBannerOverride),
      setter: (bool value) {
        if (WidgetsApp.debugAllowBannerOverride == value)
          return Future<void>.value();
        WidgetsApp.debugAllowBannerOverride = value;
        return _forceRebuild();
      },
    );

    // This service extension is deprecated and will be removed by 12/1/2018.
    // Use ext.flutter.inspector.show instead.
    registerBoolServiceExtension(
        name: 'debugWidgetInspector',
        getter: () async => WidgetsApp.debugShowWidgetInspectorOverride,
        setter: (bool value) {
          if (WidgetsApp.debugShowWidgetInspectorOverride == value)
            return Future<void>.value();
          WidgetsApp.debugShowWidgetInspectorOverride = value;
          return _forceRebuild();
        },
    );

    WidgetInspectorService.instance.initServiceExtensions(registerServiceExtension);

    return true;
  }());
}