useTextEditingController top-level constant

_TextEditingControllerHookCreator const useTextEditingController

Creates a TextEditingController, either via an initial text or an initial TextEditingValue.

To use a TextEditingController with an optional initial text, use:

final controller = useTextEditingController(text: 'initial text');

To use a TextEditingController with an optional initial value, use:

final controller = useTextEditingController
  .fromValue(TextEditingValue.empty);

Changing the text or initial value after the widget has been built has no effect whatsoever. To update the value in a callback, for instance after a button was pressed, use the TextEditingController.text or TextEditingController.value setters. To have the TextEditingController reflect changing values, you can use useEffect. This example will update the TextEditingController.text whenever a provided ValueListenable changes:

final controller = useTextEditingController();
final update = useValueListenable(myTextControllerUpdates);

useEffect(() {
  controller.text = update;
}, [update]);

See also:

Implementation

const useTextEditingController = _TextEditingControllerHookCreator();