As with a lot of postings on this blog, when I look into learning a new language I copy something I have done before. I have decided to use the Battle.net API to pull World of Warcraft data. It’s an easy subject to reason about and show off parts of the language’s syntax.
Also, as I have the same code in Ruby, Groovy, and CoffeeScript it’s a nice comparison. You can find the other code as part of the Updated World of Warcraft Armory code posting.
You will need to install the ‘http’ package from Pub to run this.
The ‘as JSON’ isn’t needed to use the JSON parse method, but I feel that it’s cleaner because ‘parse’ could be used for many things.
I like the syntax for setting up the map, but using it you can only have your keys be Strings. If you want anything else, you would need to add the items one at a time. It was easier for me to use toString on the int and keep the map in the nice syntax.
I couldn’t find a way to have the map include a default key, but as long as we don’t try to ask for anything that isn’t in the map it will be fine.
A great Dart resource is the Dart Screencasts.
Got a way to make the code better? Fork the Gist or comment on Github.
Edit: Vyacheslav Egorov on Twitter has pointed out to me that you don’t need to create a closure if you are doing something simple, like parsing the JSON.