findControlInCollection method Null safety

AbstractControl<Object>? findControlInCollection(
  1. List<String> path
)

Walks the path to find the matching control.

Returns null if no match is found.

Implementation

AbstractControl<Object>? findControlInCollection(List<String> path) {
  if (path.isEmpty) {
    return null;
  }

  final result = path.fold(this as AbstractControl<Object>, (control, name) {
    if (control is FormControlCollection<dynamic>) {
      final collection = control;
      return collection.contains(name) ? collection.control(name) : null;
    } else {
      return null;
    }
  });

  return result != null ? result as AbstractControl<Object> : null;
}