asListModel<T> method

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

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

key The key in the map to retrieve the value from. delegate A function that takes dynamic data 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 the map value, or def if not found or cannot be parsed.

Implementation

List<T> asListModel<T>(
  String key,
  T Function(dynamic data) delegate, {
  List<T>? def,
}) {
  List<T> result = [];
  try {
    if (keys.contains(key)) {
      List<dynamic> map = this[key];
      for (var value in map) {
        result.add(delegate(value));
      }

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