removeQuery method
Returns a new URI with the query removed, including its ? delimiter.
When removeFragment is true (default), the fragment and its # are
also removed.
Example:
Uri.parse('https://x.com/a?q=1#top').removeQuery(); // https://x.com/a
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
Uri removeQuery({bool removeFragment = true}) {
// replace(query: '') keeps an empty component, which still prints its
// delimiter, so both components have to be dropped rather than emptied.
Uri result = this.removeFragment();
if (result.hasQuery) {
// Uri has no removeQuery of its own. With the fragment gone, everything
// from the first '?' is the query (earlier parts percent-encode '?').
final String text = result.toString();
result = Uri.parse(text.substring(0, text.indexOf('?')));
}
// Put the original fragment back only when the caller asked to keep it.
if (removeFragment || !hasFragment) return result;
return result.replace(fragment: fragment);
}