urlencoding in Dart

var uri = 'http://example.org/api?foo=some message';
var encoded = Uri.encodeFull(uri);
assert(encoded == 'http://example.org/api?foo=some%20message');

var decoded = Uri.decodeFull(encoded);
assert(uri == decoded);

http://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-uri


Update: There is now support for encode/decode URI in the Dart Uri class

Dart's URI code is placed in a separate library called dart:uri (so it can be shared between both dart:html and dart:io). It looks like it currently does not include a urlencode function so your best alternative, for now, is probably to use this Dart implementation of JavaScript's encodeUriComponent.


I wrote this small function to convert a Map into a URL encoded string, which may be what you're looking for.

String encodeMap(Map data) {
  return data.keys.map((key) => "${Uri.encodeComponent(key)}=${Uri.encodeComponent(data[key])}").join("&");
}

Uri.encodeComponent(url); // To encode url
Uri.decodeComponent(encodedUrl); // To decode url