control method

  1. @override
AbstractControl control(
  1. String name
)
override

Retrieves a child control given the control's name or path.

The name is a dot-delimited string that represents the index position of the control in array or the path to the nested control.

Throws FormArrayInvalidIndexException if name is not e valid int number.

Throws FormControlNotFoundException if no FormControl founded with the specified name.

Example:

final array = FormArray([
  FormControl(defaultValue: 'hello'),
]);

final control = array.formControl('0');

print(control.value);
>hello

Retrieves a nested control

final form = FormGroup({
  'address': FormArray([
    FormGroup({
      'zipCode': FormControl<int>(value: 1000),
      'city': FormControl<String>(value: 'Sofia'),
    })
  ]),
});

form.control('address.0.city');

Implementation

@override
AbstractControl<dynamic> control(String name) {
  final namePath = name.split(_controlNameDelimiter);
  if (namePath.length > 1) {
    final control = findControlInCollection(namePath);
    if (control != null) {
      return control;
    }
  } else {
    final index = int.tryParse(name);
    if (index == null) {
      throw FormArrayInvalidIndexException(name);
    } else if (index < _controls.length) {
      return _controls[index];
    }
  }

  throw FormControlNotFoundException(controlName: name);
}