mapFromQueryString function
Parses a URL query string into a Map<String, String>.
mapFromQueryString('page=1&q=hello') // {'page': '1', 'q': 'hello'}
Implementation
Map<String, String> mapFromQueryString(String query) {
if (query.isEmpty) return {};
final q = query.startsWith('?') ? query.substring(1) : query;
return Map.fromEntries(
q.split('&').where((p) => p.isNotEmpty).map((pair) {
final idx = pair.indexOf('=');
if (idx == -1) {
return MapEntry(Uri.decodeQueryComponent(pair), '');
}
return MapEntry(
Uri.decodeQueryComponent(pair.substring(0, idx)),
Uri.decodeQueryComponent(pair.substring(idx + 1)),
);
}),
);
}