registerSignalServiceExtension method Null safety

  1. @protected
void registerSignalServiceExtension (
  1. {required String name,
  2. required AsyncCallback callback}
)
@protected

Registers a service extension method with the given name (full name "ext.flutter.name"), which takes no arguments and returns no value.

Calls the callback callback when the service extension is called.

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.
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.
void myRegistrationFunction() {
  // 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.

Implementation

@protected
void registerSignalServiceExtension({
  required String name,
  required AsyncCallback callback,
}) {
  assert(name != null);
  assert(callback != null);
  registerServiceExtension(
    name: name,
    callback: (Map<String, String> parameters) async {
      await callback();
      return <String, dynamic>{};
    },
  );
}