jsonDecode function

Map<String, dynamic> jsonDecode(
  1. List args
)

Provides a jsonDecode() function that decodes a JSON string to a map object. Supports conversion of DateTime objects from strings.

string String -> Map

Example

var json = jsonDecode("{\"name\": \"John\", \"age\": 30}");
print(json is Map); // true
print("JSON: $json");

Implementation

Map<String, dynamic> jsonDecode(List args) {
  _arityCheck(1, args.length);
  if (args[0] is! String) {
    throw RuntimeError(
      "jsonDecode: Expected a string, but got ${args[0].runtimeType}",
    );
  }

  return json.jsonDecode(
    args[0] as String,
    reviver: (key, value) {
      if (value is String) {
        try {
          return DateTime.parse(value);
        } catch (e) {
          return value;
        }
      } else {
        return value;
      }
    },
  );
}