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.

Implementation

@useResult
static List<Map<String, dynamic>>? tryJsonDecodeListMap(String? value) {
  if (value == null || !isJson(value)) return null;

  try {
    final Object? data = dc.json.decode(value);
    // Guarded by isEmpty check above
    // ignore: prefer_list_first
    if (data is! List || data.isEmpty || data[0] 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) {
    // debugPrint is appropriate for utility packages (stripped in release builds, no external dependencies)
    // ignore: saropa_lints/avoid_print_error
    debugPrint('JsonUtils.tryJsonDecodeListMap failed: $e\n$stackTrace');

    return null;
  }
}