parseNestedQuery function

Map<String, Object?> parseNestedQuery(
  1. String queryString
)

Parses queryString into a nested map. Nested keys use bracket notation; all values are strings.

Implementation

Map<String, Object?> parseNestedQuery(String queryString) {
  final Map<String, Object?> root = <String, Object?>{};
  if (queryString.isEmpty) return root;
  for (final String pair in queryString.split('&')) {
    final int eq = pair.indexOf('=');
    if (eq < 0) continue;
    final String keyStr = Uri.decodeComponent(pair.substringSafe(0, eq));
    final String value = Uri.decodeComponent(pair.substringSafe(eq + 1));
    // Bracket notation a[b][c] becomes ['a','b','c']: dropping every ']' then
    // splitting on '[' yields one segment per nesting level without a regex.
    final List<String> keySegments = keyStr.replaceAll(']', '').split('[');
    Map<String, Object?> current = root;
    // Walk all but the final segment, creating intermediate maps as we descend;
    // the last segment is the leaf that actually holds the value (set below).
    for (int i = 0; i < keySegments.length - 1; i++) {
      final String k = keySegments[i];
      final child = current.putIfAbsent(k, () => <String, Object?>{});
      if (child is Map<String, Object?>) current = child;
    }
    final lastKey = keySegments.lastOrNull;
    if (lastKey != null) current[lastKey] = value;
  }
  return root;
}