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 and logged if debug is enabled.

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) {
    try {
      return v.map((item) => mapper(item)).toList();
    } catch (e) {
      _log("Error mapping list items: $e");
    }
  } else if (v != null) {
    _logMismatchedType("List", v);
  }
  return <T>[];
}