decodeJsonText function

Object? decodeJsonText(
  1. Object? raw
)

Decodes raw when it's JSON sent as text, e.g. '{"a": 1}' to a map; anything else comes back unchanged.

Implementation

Object? decodeJsonText(Object? raw) {
  if (raw is! String) return raw;
  final text = raw.trim();
  if (!text.startsWith('{') && !text.startsWith('[')) return raw;
  try {
    return jsonDecode(text);
  } on FormatException {
    return raw;
  }
}