restfulUrl static method

String restfulUrl(
  1. String url,
  2. Map<String, dynamic> params
)

Implementation

static String restfulUrl(String url, Map<String, dynamic> params) {
  String resultUrl = url;
  List<String> keysToRemove = [];

  params.forEach((key, value) {
    String placeholder = "{$key}";
    if (resultUrl.contains(placeholder)) {
      resultUrl = resultUrl.replaceAll(placeholder, Uri.encodeComponent(value.toString()));
      keysToRemove.add(key);
    }
  });

  // 从 params Map 中移除已用于路径替换的键
  for (String key in keysToRemove) {
    params.remove(key);
  }
  // Normalize slashes after replacements
  // First, handle the scheme part if present to avoid mangling http:// to http:/
  int schemeEndIndex = resultUrl.indexOf("://");
  String scheme = "";
  String rest = resultUrl;

  if (schemeEndIndex != -1) {
    scheme = resultUrl.substring(0, schemeEndIndex + 3);
    rest = resultUrl.substring(schemeEndIndex + 3);
  }

  rest = rest.replaceAll(RegExp(r'/+'), '/');
  return scheme + rest;
}