tryJsonDecodeListMap static method

  1. @useResult
List<Map<String, dynamic>>? tryJsonDecodeListMap(
  1. String? value
)

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

Implementation

@useResult
static List<Map<String, dynamic>>? tryJsonDecodeListMap(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);
    // firstOrNull replaces the old data[0] index access: it never throws, and
    // the isEmpty check before it already returns null for an empty list.
    if (data is! List || data.isEmpty || data.firstOrNull is! Map<String, dynamic>) {
      return null;
    }

    if (!data.every((Object? e) => e is Map<String, dynamic>)) {
      return null;
    }

    return data.cast<Map<String, dynamic>>().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.tryJsonDecodeListMap failed', error: e, stackTrace: stackTrace);

    return null;
  }
}