How do you set the value of a TextField in Flutter

0 votes
I'd like to supply an initial value to a text field and redraw it with an empty value to clear the text. What's the best approach to do that with Flutter's APIs?
Mar 6, 2023 in Android by Tejashwini
• 780 points
• 21,088 views

1 answer to this question.

0 votes

To supply an initial value to a text field in Flutter, you can set the controller property of the TextField widget to a TextEditingController that contains the initial value. Here's an example:

TextEditingController _controller = TextEditingController(text: 'Initial Value');

TextField(
  controller: _controller,
  onChanged: (text) {
    // Do something when the text changes
  },
);

To clear the text field, you can call the clear() method of the TextEditingController instance. For example:

_controller.clear();

This will clear the text in the text field and trigger a redraw.

Alternatively, you can also set the value property of the TextEditingController to a TextEditingValue with an empty string to clear the text. Here's an example:

_controller.value = TextEditingValue(text: '');

This will also clear the text and trigger a redraw.

answered Mar 9, 2023 by pooja

Related Questions In Android

0 votes
0 answers

How purple 200/500/700 define in the color.xml of a new Android Project?

A new Android project provides a set ...READ MORE

Nov 23, 2022 in Android by Ashwini
• 5,440 points
• 4,735 views
0 votes
1 answer

How do I disable a Button in Flutter?

To disable a button in Flutter, you ...READ MORE

answered Mar 10, 2023 in Android by vinayak
• 22,005 views
0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,800 points
• 1,504 views
0 votes
0 answers

How do I get the current GPS location programmatically in Android?

I need to get my current location ...READ MORE

Nov 23, 2022 in Android by Ashwini
• 5,440 points
• 1,933 views
0 votes
1 answer

How to create number input field in Flutter?

Yes, Flutter has a built-in widget called ...READ MORE

answered Mar 1, 2023 in Android by vishalini
• 12,594 views
0 votes
1 answer

How to make a TextField with HintText but no Underline?

You can achieve this by using the ...READ MORE

answered Mar 21, 2023 in Flutter by Anushi
• 14,029 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

save the code and it will be ...READ MORE

answered Jun 8, 2022 in Others by anonymous

edited Mar 5, 2025 • 5,527 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
• 2,646 views
0 votes
1 answer

How can I remove the debug banner in Flutter?

The debug banner in Flutter can be ...READ MORE

answered Mar 1, 2023 in Android by vishalini
• 2,490 views
0 votes
1 answer

How to do Rounded Corners Image in Flutter?

To make the image in your Flutter ...READ MORE

answered Mar 1, 2023 in Android by vishalini
• 3,709 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP