tryDecode static method

Object? tryDecode(
  1. String source
)

Decodes a JSON string, otherwise returns null.

Implementation

static Object? tryDecode(String source) {
  final trimmed = source.trim();
  if (trimmed.isEmpty) return null;
  final first = trimmed.codeUnitAt(0);
  // only check strings starting with `{` or `[`
  if (first != 0x7B && first != 0x5B) return null;
  try {
    return jsonDecode(trimmed) as Object?;
  } catch (_) {
    return null;
  }
}