parseQueryString function

Map<String, String> parseQueryString(
  1. String location
)

Parses a URL or query string into a single-value map of query parameters.

Keys and values are percent-decoded, + is converted to spaces, and duplicate keys follow "last-wins" semantics. Keys without values are assigned an empty string. Malformed percent escapes remain literal; invalid UTF-8 bytes decode as U+FFFD so untrusted URL input cannot crash route resolution. If no query string is present, returns an empty map.

Safe for both server-side rendering (SSR) and browser environments.

final query = parseQueryString('/search?q=shoes+sale&category=footwear');
print(query['q']); // 'shoes sale'
print(query['category']); // 'footwear'

Implementation

Map<String, String> parseQueryString(String location) {
  final all = parseQueryStringAll(location);
  if (all.isEmpty) return const {};
  return {for (final entry in all.entries) entry.key: entry.value.last};
}