getError<T> method

T? getError<T>(
  1. String propertyName
)

Implementation

T? getError<T>(String propertyName) {
  if (propertyName.isEmptyOrWhiteSpace) return null;
  propertyName = propertyName.trim();

  if (!propertyName.contains(".")) {
    if (this._errors.containsKey(propertyName))
      return this._errors[propertyName] as T?;
    return null;
  }

  final split = propertyName.split(".");
  dynamic current = this._errors;

  for (var i = 0; i < split.length; i++) {
    if (current == null) return null;
    if (current is Map<String, dynamic>) {
      if (!current.containsKey(split[i])) return null;
      current = current[split[i]];
      continue;
    }
    if (current is ValidationErrors)
      return current.getError(split.sublist(i).join("."));

    // if the top level validation fails then error blow is thrown.
    // validate the top level first before validating a second level error.

    throw Exception(
        "In Map $this the value for key = ${split.getRange(0, i).join(".")} expected Map<String, dynamic> or ValidationErrors got ${current.runtimeType} [$current]");
  }

  return current as T?;
}