Extract parameter / value pairs from an Uri as a Map


How to get the parameter / value pairs of an URL / URI using Dart? Unfortunately currently there is no built-in functionality for this problem neither in the Uri library or the Location interface.

Map<String, String> getUriParams(String uriSearch) {
    if (uriSearch != '') {
      final List<String> paramValuePairs = uriSearch.substring(1).split('&');

      var paramMapping = new HashMap<String, String>();
      paramValuePairs.forEach((e) {
        if (e.contains('=')) {
          final paramValue = e.split('=');
          paramMapping[paramValue[0]] = paramValue[1];
        } else {
          paramMapping[e] = '';
        }
      });
      return paramMapping;
    }
  }

// Uri: http://localhost:8080/incubator/main.html?param=value&param1&param2=value2&param3
  final uriSearch = window.location.search;
  final paramMapping = getUriParams(uriSearch);

  paramMapping.forEach((k, v) => print('$k:$v'));

Comments

Popular posts from this blog

Apple's evaluating a new JVM

Tuning ext4 for performance with emphasis on SSD usage