setExtensionEnabledState method

ExtensionEnabledState setExtensionEnabledState({
  1. required Uri devtoolsOptionsUri,
  2. required String extensionName,
  3. required bool enable,
})

Sets the enabled state for extensionName in the 'devtools_options.yaml' file at devtoolsOptionsUri.

If the 'devtools_options.yaml' file does not exist, it will be created.

devtoolsOptionsUri is expected to be a file:// URI.

Implementation

ExtensionEnabledState setExtensionEnabledState({
  required Uri devtoolsOptionsUri,
  required String extensionName,
  required bool enable,
}) {
  final options = _optionsAsMap(optionsUri: devtoolsOptionsUri);
  if (options == null) return ExtensionEnabledState.error;

  var extensions =
      (options[_extensionsKey] as List?)?.cast<Map<String, Object?>>();
  if (extensions == null) {
    options[_extensionsKey] = <Map<String, Object?>>[];
    extensions = options[_extensionsKey] as List<Map<String, Object?>>;
  }

  // Write the new enabled state to the map.
  final extension = extensions.firstWhereOrNull(
    (e) => e.keys.first == extensionName,
  );
  if (extension == null) {
    extensions.add({extensionName: enable});
  } else {
    extension[extensionName] = enable;
  }

  _writeToOptionsFile(optionsUri: devtoolsOptionsUri, options: options);

  // Lookup the enabled state from the file we just wrote to to ensure that
  // are not returning an out of sync result.
  return lookupExtensionEnabledState(
    devtoolsOptionsUri: devtoolsOptionsUri,
    extensionName: extensionName,
  );
}