Retrieving parameter / value pairs from Uri in map form

How do I get URL / URI parameter / value pairs using Dart? Unfortunately, there are currently no built-in functions for this problem in either the Uri library or the Location interface.

+6
source share
4 answers

There is currently a queryParameters member from Uri that returns a map

Uri u = Uri.parse("http://app.org/main?foo=bar&baz=bat"); Map<String,String> qp = u.queryParameters; print(qp); // {foo: bar, baz: bat} 
+2
source

You can use Uri.splitQueryString to split the query into a map.

+2
source
  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); 
0
source
  // url=http://127.0.0.1:3030/path/Sandbox.html?paramA=1&parmB=2#myhash void main() { String querystring = window.location.search.replaceFirst("?", ""); List<String> list = querystring.split("&").forEach((e) => e.split("=")); print(list); // [[paramA, 1], [parmB, 2]] } 
0
source

Source: https://habr.com/ru/post/921925/


All Articles