finishAutofillContext static method
- bool shouldSave = true,
Finishes the current autofill context, and potentially saves the user
input for future use if shouldSave
is true.
Typically, this method should be called when the user has finalized their input. For example, in a Form, it's typically done immediately before or after its content is submitted.
The topmost AutofillGroups also call finishAutofillContext automatically when they are disposed. The default behavior can be overridden in AutofillGroup.onDisposeAction.
An autofill context is a collection of input fields that live in the
platform's text input plugin. The platform is encouraged to save the user
input stored in the current autofill context before the context is
destroyed, when TextInput.finishAutofillContext is called with
shouldSave
set to true.
Currently, there can only be at most one autofill context at any given time. When any input field in an AutofillGroup requests for autofill (which is done automatically when an autofillable EditableText gains focus), the current autofill context will merge the content of that AutofillGroup into itself. When there isn't an existing autofill context, one will be created to hold the newly added input fields from the group.
Once added to an autofill context, an input field will stay in the context
until the context is destroyed. To prevent leaks, call
TextInput.finishAutofillContext to signal the text input plugin that the
user has finalized their input in the current autofill context. The
platform text input plugin either encourages or discourages the platform
from saving the user input based on the value of the shouldSave
parameter. The platform usually shows a "Save for autofill?" prompt for
user confirmation.
On many platforms, calling finishAutofillContext shows the save user input dialog and disrupts the user's flow. Ideally the dialog should only be shown no more than once for every screen. Consider removing premature finishAutofillContext calls to prevent showing the save user input UI too frequently. However, calling finishAutofillContext when there's no existing autofill context usually does not bring up the save user input UI.
See also:
- EditableText.autofillHints for autofill save troubleshooting tips.
- AutofillGroup.onDisposeAction, a configurable action that runs when a topmost AutofillGroup is getting disposed.
Implementation
static void finishAutofillContext({ bool shouldSave = true }) {
for (final TextInputControl control in TextInput._instance._inputControls) {
control.finishAutofillContext(shouldSave: shouldSave);
}
}