buildUri static method

Uri buildUri(
  1. String hostAddress, {
  2. String? path,
  3. Map? queryArgs,
})

Implementation

static Uri buildUri(String hostAddress, {String? path, Map? queryArgs}) {
  var uri = hostAddress;
  if (path != null && path.isNotEmpty) {
    if (hostAddress.endsWith("/") && path.startsWith("/")) {
      path = path.substring(1);
    } else if (!hostAddress.endsWith("/") && !path.startsWith("/")) {
      uri += "/";
    }
    uri += path;
  }

  if (queryArgs != null) {
    var leadingChar = uri.contains("?") ? "&" : "?";
    for (var key in queryArgs.keys) {
      var value = queryArgs[key];
      if (value == null || value.toString().isEmpty) continue;
      uri += "$leadingChar$key=$value";
      leadingChar = "&";
    }
  }
  return Uri.parse(uri);
}