parseQueryString function

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

Parse query string to map (and back). Roadmap #168. Audited: 2026-06-12 11:26 EDT

Implementation

Map<String, String> parseQueryString(String query) {
  final Map<String, String> out = <String, String>{};
  if (query.isEmpty) return out;
  // Split on '&' into pairs, then on the FIRST '=' into key/value so a value
  // containing '=' is preserved. Both sides are percent-decoded.
  for (final String pair in query.split('&')) {
    final int eq = pair.indexOf('=');
    if (eq != -1) {
      out[_decode(pair.replaceRange(eq, pair.length, ''))] = _decode(
        pair.replaceRange(0, eq + 1, ''),
      );
    } else if (pair.isNotEmpty) {
      // A bare token with no '=' is a flag key with an empty value.
      out[_decode(pair)] = '';
    }
  }
  return out;
}