normalizeVmServiceUri function

Uri? normalizeVmServiceUri(
  1. String value

Returns a normalized vm service uri.

Removes trailing characters, trailing url fragments, and decodes urls that were accidentally encoded.

For example, given a value of, this method will return the URI

Returns null if the Uri parsed from value is not Uri.absolute (ie, it has no scheme or it has a fragment).


Uri? normalizeVmServiceUri(String value) {
  value = value.trim();

  // Clean up urls that have a devtools server's prefix, aka:
  const uriParamToken = '?uri=';
  if (value.contains(uriParamToken)) {
    value = value.substring(
      value.indexOf(uriParamToken) + uriParamToken.length,

  // Cleanup encoded urls likely copied from the uri of an existing running
  // DevTools app.
  if (value.contains('%3A%2F%2F')) {
    value = Uri.decodeFull(value);
  final uri = Uri.parse(value.trim()).removeFragment();
  if (!uri.isAbsolute) {
    return null;
  if (uri.path.endsWith('/')) return uri;
  return uri.replace(path: uri.path);