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);
    // ignore: saropa_lints/prefer_list_first -- index access is clearer alongside the adjacent data.isEmpty short-circuit guard
    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) {
    // ignore: saropa_lints/avoid_print_error -- intentional diagnostic logging; debugPrint is stripped in release builds
    debugPrint('JsonUtils.tryJsonDecodeListMap failed: $e\n$stackTrace');

    return null;
  }
}