list<T> static method

List<T> list<T>(
  1. dynamic v,
  2. T mapper(
    1. dynamic
    )
)

Safely parses a dynamic value v as a List of items using a mapper function.

Returns an empty list if v is not a List. Does not throw if mapping fails for individual items; errors are caught per item and logged if debug is enabled, retaining remaining valid items.

Example:

JsonCare.list(json['tags'], (i) => JsonCare.string(i));

Implementation

static List<T> list<T>(dynamic v, T Function(dynamic) mapper) {
  if (v is List) {
    final result = <T>[];
    for (final item in v) {
      try {
        result.add(mapper(item));
      } catch (e) {
        _log("Error mapping list item ($item): $e");
      }
    }
    return result;
  } else if (v != null) {
    _logMismatchedType("List", v);
  }
  return <T>[];
}