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.

Implementation

@useResult
static List<String>? tryJsonDecodeList(String? value) {
  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) {
    // ignore: saropa_lints/avoid_print_error -- intentional diagnostic logging; debugPrint is stripped in release builds
    debugPrint('JsonUtils.tryJsonDecodeList failed: $e\n$stackTrace');

    return null;
  }
}