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 in Android by Tejashwini
• 780 points
1,138 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 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,560 points
166 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
152 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,560 points
173 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
110 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 in Flutter by Anushi
238 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
2,957 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
655 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,613 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 in Android by vishalini
317 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 in Android by vishalini
545 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