patchValue method Null safety

  1. @override
void patchValue(
  1. Map<String, Object?>? value,
  2. {bool updateParent = true,
  3. bool emitEvent = true}
)
override

Patches the value of the FormGroup. It accepts an object with control names as keys, and does its best to match the values to the correct controls in the group.

It accepts both super-sets and sub-sets of the group.

The value argument matches the structure of the group, with control names as keys.

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 form = FormGroup({
  'name': FormControl<String>(value: 'John'),
  'email': FormControl<String>(value: 'john@email.com'),
});

print(form.value); // outputs: {name: 'John', email: 'john@email.com'}

form.patchValue({'name': 'Doe'});

print(form.value); // outputs: {name: 'Doe', email: 'john@email.com'}

Implementation

@override
void patchValue(
  Map<String, Object?>? value, {
  bool updateParent = true,
  bool emitEvent = true,
}) {
  value?.forEach((name, value) {
    if (_controls.containsKey(name)) {
      _controls[name]!.patchValue(
        value,
        updateParent: false,
        emitEvent: emitEvent,
      );
    }
  });

  updateValueAndValidity(
    updateParent: updateParent,
    emitEvent: emitEvent,
  );
}