asListModelLess<T> method

List<T> asListModelLess<T>(
  1. String key,
  2. T delegate(
    1. ModelLess data
    ), {
  3. List<T>? def,
})

Retrieves a list of models of type T from the map using ModelLess instances and a provided delegate function to parse each item.

key The key in the map to retrieve the value from. delegate A function that takes a ModelLess instance and returns an instance of type T. def The default value to return if the key is not found or the value cannot be parsed. Defaults to an empty list if not provided.

Returns: A list of models of type T parsed from ModelLess instances in the map value, or def if not found or cannot be parsed.

Implementation

List<T> asListModelLess<T>(
  String key,
  T Function(ModelLess data) delegate, {
  List<T>? def,
}) {
  List<T> result = [];
  try {
    if (keys.contains(key)) {
      ModelLessArray map = this[key];
      map.forEach<ModelLess>((val) {
        result.add(delegate(val));
      });

      return result;
    }
  } catch (e) {
    return def ?? [];
  }
  return def ?? [];
}