registerServiceExtension method

  1. @override
  2. @protected
void registerServiceExtension({
  1. required String name,
  2. required ServiceExtensionCallback callback,
})
inherited

Registers a service extension method with the given name (full name "ext.flutter.name").

The given callback is called when the extension method is called. The callback must return a Future that either eventually completes to a return value in the form of a name/value map where the values can all be converted to JSON using json.encode() (see JsonEncoder), or fails. In case of failure, the failure is reported to the remote caller and is dumped to the logs.

The returned map will be mutated.

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

{@tool snippet} The following code registers a service extension that is only included in debug builds.

void myRegistrationFunction() {
  assert(() {
    // Register your service extension here.
    return true;
  }());
}

{@end-tool}

{@tool snippet} A service extension registered with the following code snippet is available in debug and profile mode.

void myOtherRegistrationFunction() {
  // kReleaseMode is defined in the 'flutter/foundation.dart' package.
  if (!kReleaseMode) {
    // Register your service extension here.
  }
}

{@end-tool}

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

Implementation

@override
@protected
void registerServiceExtension({
  required String name,
  required ServiceExtensionCallback callback,
}) {}