reset method

  1. @override
void reset({
  1. T? value,
  2. bool overwriteDefaultValue = false,
  3. bool updateParent = true,
  4. bool emitEvent = true,
  5. bool removeFocus = false,
  6. bool? disabled,
})
override

Resets the form control, marking it as untouched and pristine.

If value is provided, the control is reset to that value.

If value is not provided (null), the behavior depends on the nonNullable argument passed to the constructor:

  • If nonNullable is true (the default), the control resets to the initial value provided in the constructor.
  • If nonNullable is false, the control resets to null.

If overwriteDefaultValue is true, then the value used to reset the control becomes the new default value of the control.

The argument disabled is optional and resets the disabled status of the control. If value is true then it will disable the control, if value is false then it will enable the control, and if the value is null or not set (the default) then the control will state in the same state that it previously was.

The argument removeFocus is optional and remove the UI focus from 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 events notify listeners with the latest status and value when the control is reset. When false, no events are emitted.

Examples

Reset to a specific value

final control = FormControl<String>();

control.reset(value: 'John Doe');

print(control.value); // output: 'John Doe'

Reset to initial value (nonNullable: true)

// nonNullable is true by default
final control = FormControl<String>(value: 'Initial Value');

control.value = 'New Value';

// Resets to 'Initial Value' because no value was provided
// and nonNullable is true.
control.reset();

print(control.value); // output: 'Initial Value'

Reset to null (nonNullable: false)

final control = FormControl<String>(
  value: 'Initial Value',
  nonNullable: false,
);

control.value = 'New Value';

// Resets to null because no value was provided
// and nonNullable is false.
control.reset();

print(control.value); // output: null

Implementation

@override
void reset({
  T? value,
  bool overwriteDefaultValue = false,
  bool updateParent = true,
  bool emitEvent = true,
  bool removeFocus = false,
  bool? disabled,
}) {
  if (overwriteDefaultValue) {
    _defaultValue = value;
  }

  super.reset(
    value: value ?? _defaultValue,
    updateParent: updateParent,
    emitEvent: emitEvent,
    removeFocus: removeFocus,
    disabled: disabled,
  );
}