Skip to content

Instantly share code, notes, and snippets.

@danielgomezrico
Last active August 24, 2022 20:22
Show Gist options
  • Save danielgomezrico/f0af61d40f37360e051e7bedde273541 to your computer and use it in GitHub Desktop.
Save danielgomezrico/f0af61d40f37360e051e7bedde273541 to your computer and use it in GitHub Desktop.
Dart - how to create a url with query params with language features
// Use language features to build the url
String buildUrlWithQueryParams(String url, Map<String, dynamic> queryParams) {
final uri = Uri.parse(url);
final fullUri = uri.replace(
queryParameters: {
...uri.queryParameters,
...queryParams,
},
);
final parsedUrl = fullUri.toString();
if (parsedUrl[parsedUrl.length - 1] == '?') {
return parsedUrl.substring(0, parsedUrl.length - 1);
} else {
return parsedUrl;
}
}
void main() {
final url = buildUrlWithQueryParams("www.google.com?name=ok%C3%BCn-living-cancun", {'param': 'value'});
print(url); // www.google.com?name=ok%C3%BCn-living-cancun&param=value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment