resetState method

void resetState(
  1. Map<String, ControlState<Object>> state, {
  2. bool removeFocus = false,
})

Resets the FormGroup, marks all descendants as untouched, and sets the value of all descendants to null.

You reset to a specific form state by passing in a map of states that matches the structure of your form, with control names as keys. The control state is an object with both a value and a disabled status.

The argument removeFocus is optional and remove the UI focus from all descendants.

Reset the form group values and disabled status

final form = FormGroup({
  'first': FormControl('first name'),
  'last': FormControl('last name'),
});

form.resetState({
  'first': ControlState(value: 'name', disabled: true),
  'last': ControlState(value: 'last'),
});

print(form.value);  // output: {first: 'name', last: 'last name'}
print(form.control('first').disabled);  // output: true

Implementation

void resetState(Map<String, ControlState<Object>> state,
    {bool removeFocus = false}) {
  if (state.isEmpty) {
    reset(removeFocus: removeFocus);
  } else {
    _controls.forEach((name, control) {
      control.reset(
        value: state[name]?.value,
        disabled: state[name]?.disabled,
        removeFocus: removeFocus,
        updateParent: false,
      );
    });
    updatePristine();
    updateValueAndValidity();
  }
}