resolveStringUri static method

Uri? resolveStringUri(
  1. String s
)

Tries to resolves a String s to an Uri.

Implementation

static Uri? resolveStringUri(String s) {
  if (!_regExpSpaces.hasMatch(s)) {
    if (s.startsWith('http://') ||
        s.startsWith('https://') ||
        s.startsWith('file:/')) {
      try {
        return Uri.parse(s);
      } catch (_) {}
    }

    var u = Uri.base;

    String? path;

    if (s.startsWith('/')) {
      path = s;
    } else if (s.startsWith('./')) {
      var basePath = u.path;
      if (!basePath.endsWith('/')) {
        basePath += '/';
      }
      path = basePath + s.substring(2);
    } else if (_regexpStartWithWord.hasMatch(s)) {
      var basePath = u.path;
      if (!basePath.endsWith('/')) {
        basePath += '/';
      }
      path = u.path + s;
    }

    if (path != null) {
      return Uri(
          scheme: u.scheme,
          userInfo: u.userInfo,
          host: u.host,
          port: u.port,
          path: path,
          query: u.query);
    }
  }

  return null;
}