Home > Net >  Flutter - How to clear TextFormField after form save/submit
Flutter - How to clear TextFormField after form save/submit

Time:01-17

With a Flutter form that uses TextFormField, how do I clear the text field after the form have been saved/submitted?

Do I have to use a Text controller?

CodePudding user response:

First define controller:

TextEditingController controller = TextEditingController();

then use it like this:

TextFormField(
   controller: controller,
   onFieldSubmitted: (value) {
     controller.clear();
   },
),

here in onFieldSubmitted you can get the value and save it in somewhere else and then clear the controller and by that TextFormField will get clear.

CodePudding user response:

If you are using a Form widget, add a Key to it, and after submitting the form you can do: _formKey.currentState?.reset(); This will reset the status of the TextFormFields that are contained in your Form.

  • Related