How do I supply an initial value to a text field

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
3,959 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 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,620 points
211 views
0 votes
0 answers

How do I make a splash screen?

I added a splash screen because I ...READ MORE

Nov 9, 2022 in Android by Edureka
• 12,690 points
208 views
0 votes
0 answers

How do I get an apk file from an Android device?

How can I download the apk file ...READ MORE

Nov 15, 2022 in Android by Edureka
• 13,620 points
232 views
0 votes
0 answers

What do I have to do with the A Href link?

How do I set up the anchor ...READ MORE

Nov 16, 2022 in Android by Edureka
• 12,690 points
213 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
973 views
0 votes
1 answer

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

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
3,137 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,440 points
735 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
3,780 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
2,329 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
814 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