registerStringServiceExtension method

  1. @protected
void registerStringServiceExtension(
  1. {required String name,
  2. required AsyncValueGetter<String> getter,
  3. required AsyncValueSetter<String> setter}

Registers a service extension method with the given name (full name ""), which optionally takes a single argument with the name "value". If the argument is omitted, the value is to be read, otherwise it is to be set. Returns the current value.

Calls the getter callback to obtain the value when responding to the service extension method being called.

Calls the setter callback with the new value when the service extension method is called with a new value.

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


void registerStringServiceExtension({
  required String name,
  required AsyncValueGetter<String> getter,
  required AsyncValueSetter<String> setter,
}) {
    name: name,
    callback: (Map<String, String> parameters) async {
      if (parameters.containsKey('value')) {
        await setter(parameters['value']!);
        _postExtensionStateChangedEvent(name, await getter());
      return <String, dynamic>{'value': await getter()};