When you fetch data from a service, you will often get it as JSON. For these cases, Dart has a dedicated JSON class, which lives in the Dart SDK library dart:convert. Here we will focus mainly on its static method decode. Say another page or handler returns this string: {"web":"pacman"}. How do you get the value of the “variable” web?
JSON.decode returns the dynamic type. I assumed it would behave like any in TypeScript, but I was wrong, which is why JSON.decode(...).web threw an exception. You need to cast dynamic to one of the Dart types that JSON.decode actually returns. In my case it was Map (other types include List, String, and so on), and you then work with it like an array, so the code looks something like this:
Map dartPage = JSON.decode('{"web":"pacman"}') as Map;
String domain = dartPage["web"];If you don’t know how to fetch data from a web service in Dart, take a look at my earlier article: 👻 The page has moved and left no forwarding address.