I have a TextField
. I want its text not to be empty. (so I want to know if the text is empty)
I have tried using the following code, but it doesn’t work:
controller.text.trim().isEmpty()
My code:
TextFormField(
controller: controller,
),
controller.text.trim().isEmpty()
How to continuously get whether the TextField
‘s text is empty in Flutter? I would appreciate any help. Thank you in advance!
3
Answers
You can add listener to your
TextEditingController
and call setState to update the UI.The place you will use
controller.text.trim().isEmpty()
will show the updated state.Example
It can be done without any temporary variable using
ValueListenableBuilder
After some research figured out
controller.text
by itself is notlistenable
TextEditingController
extendsValueNotifier<TextEditingValue>
i.e you can useValueListenableBuilder
from material package to listen to text changes.Code:
Without text:
With text: