getField<K> static method

K getField<K>(
  1. Map<String, dynamic> map,
  2. String fieldName, [
  3. bool emptyIsNull = true
])

Gets a field out of a Map of the given type. Throws a BadTypeException if the type cannot be cast. Throws a MissingFieldException if the field does not exist and K is not nullable

Implementation

static K getField<K>(Map<String, dynamic> map, String fieldName, [bool emptyIsNull = true]) {

  if (map.containsKey(fieldName)) {

    if (map[fieldName] is K) {
      return map[fieldName] as K;
    }
    throw BadTypeException(fieldName, K.runtimeType, map[fieldName].runtimeType);

  } else {
    if (null is K && emptyIsNull) {
      return null as K;
    }
    throw MissingFieldException(fieldName, emptyIsNull, null is K);
  }

}