splitQueryString static method
Splits the query
into a map according to the rules
specified for FORM post in the HTML 4.01 specification section
17.13.4.
Each key and value in the returned map has been decoded. If the query
is the empty string, an empty map is returned.
Keys in the query string that have no value are mapped to the empty string.
Each query component will be decoded using encoding
. The default
encoding is UTF-8.
Example:
final queryStringMap =
Uri.splitQueryString('limit=10&max=100&search=Dart%20is%20fun');
print(jsonEncode(queryStringMap));
// {"limit":"10","max":"100","search":"Dart is fun"}
Implementation
static Map<String, String> splitQueryString(String query,
{Encoding encoding = utf8}) {
return query.split("&").fold({}, (map, element) {
int index = element.indexOf("=");
if (index == -1) {
if (element != "") {
map[decodeQueryComponent(element, encoding: encoding)] = "";
}
} else if (index != 0) {
var key = element.substring(0, index);
var value = element.substring(index + 1);
map[decodeQueryComponent(key, encoding: encoding)] =
decodeQueryComponent(value, encoding: encoding);
}
return map;
});
}