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 inital value, use

final controller = useTextEditingController

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.text 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;
  return null; // we don't need to have a special dispose logic
}, [update]);

See also:

  • TextEditingController, which this hook creates.


const useTextEditingController = _TextEditingControllerHookCreator()