updateValue method
void
updateValue(})
override
Sets the value of the FormArray.
The value
argument is a collection that matches the structure of the
control.
When updateParent
is true or not supplied (the default) each change
affects this control and its parent, otherwise only affects to this
control.
When emitEvent
is true or not supplied (the default), both the
statusChanges and valueChanges emit events with the latest status
and value when the control is reset. When false, no events are emitted.
Example:
final array = FormArray([
FormControl(),
FormControl(),
]);
print(array.value); // outputs: [null, null]
array.updateValue(['John', 'Doe']);
print(array.value); // outputs: ['John', 'Doe']
Implementation
@override
void updateValue(
List<T?>? value, {
bool updateParent = true,
bool emitEvent = true,
}) {
for (var i = 0; i < _controls.length; i++) {
if (value == null || i < value.length) {
_controls[i].updateValue(
value?.elementAt(i),
updateParent: false,
emitEvent: emitEvent,
);
}
}
if (value != null && value.length > _controls.length) {
final newControls = value
.toList()
.asMap()
.entries
.where((entry) => entry.key >= _controls.length)
.map((entry) => FormControl<T>(value: entry.value))
.toList();
addAll(
newControls,
updateParent: updateParent,
emitEvent: emitEvent,
);
} else {
updateValueAndValidity(
updateParent: updateParent,
emitEvent: emitEvent,
);
}
}