url method

String url(
  1. String subPath, {
  2. Map<String, String>? params,
})

Constructs a full URL by combining a base URL with a subpath and optional query parameters.

subPath - The path to be appended to the base URL. params - Optional query parameters to be included in the URL.

Returns a String containing the full URL.

Implementation

String url(String subPath, {Map<String, String>? params}) {
  var pathRequest = _rq.requestedUri.origin;
  var uri = Uri.parse(pathRequest);
  uri = uri.resolve(subPath);
  if (params != null) {
    uri = uri.replace(queryParameters: params);
  }

  /// When the request is HTTPS, the URL should be HTTPS as well.
  var port = _rq.uri.port;
  uri = uri.replace(scheme: uri.scheme);
  if (![80, 443, 0].contains(port)) {
    uri = uri.replace(port: port);
  }
  var url = uri.toString();
  return url;
}