getArgFromMap<T> static method

T? getArgFromMap<T>(
  1. Map? map, {
  2. dynamic key,
  3. bool predicate(
    1. dynamic
    )?,
  4. T? defaultValue,
})

Tries to return item of given key, Type or predicate. If key is not specified, then Parse.getArgFromList is used. If none found, then defaultValue is returned.

Implementation

static T? getArgFromMap<T>(Map? map,
    {dynamic key, bool Function(dynamic)? predicate, T? defaultValue}) {
  if (map == null) {
    return defaultValue;
  }

  if (key != null) {
    if (map.containsKey(key)) {
      return map[key];
    }

    if (key is Type) {
      final item = map.values
          .nullable()
          .firstWhere((item) => item.runtimeType == key, orElse: () => null);

      if (item != null) {
        return item;
      }
    }

    if (predicate == null) {
      return defaultValue;
    }
  }

  if (T != dynamic && predicate == null) {
    final item = map.values
        .nullable()
        .firstWhere((item) => item is T, orElse: () => null);

    if (item != null) {
      return item;
    }
  }

  return getArgFromList<T>(map.values,
      predicate: predicate, defaultValue: defaultValue);
}