tryJsonDecodeList static method

  1. @useResult
List<String>? tryJsonDecodeList(
  1. String? value
)

Returns a list of strings decoded from the JSON string value, or null if decoding fails. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
static List<String>? tryJsonDecodeList(String? value) {
  // Analyzer false positive: the result is used in this condition.
  // ignore: unused_result
  if (value == null || !isJson(value)) return null;

  try {
    final Object? data = dc.json.decode(value);
    if (data is! List || data.isEmpty) return null;
    if (!data.every((Object? e) => e is String)) return null;

    return data.cast<String>().toList();
  } on FormatException catch (e, stackTrace) {
    // Structured log instead of debugPrint, so the stack trace is not printed
    // to stdout; the caller still gets null as the failure signal.
    log('JsonUtils.tryJsonDecodeList failed', error: e, stackTrace: stackTrace);

    return null;
  }
}