362

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?

Seth Ladd
  • 112,095
  • 66
  • 196
  • 279

19 Answers19

264

You can use a TextFormField instead of TextField, and use the initialValue property. for example

TextFormField(initialValue: "I am smart")
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Sami Kanafani
  • 14,244
  • 6
  • 45
  • 41
212

(From the mailing list. I didn't come up with this answer.)

class _FooState extends State<Foo> {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController(text: 'Initial value');
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
        new TextField(
          // The TextField is first built, the controller has some initial text,
          // which the TextField shows. As the user edits, the text property of
          // the controller is updated.
          controller: _controller,
        ),
        new RaisedButton(
          onPressed: () {
            // You can also use the controller to manipuate what is shown in the
            // text field. For example, the clear() method removes all the text
            // from the text field.
            _controller.clear();
          },
          child: new Text('CLEAR'),
        ),
      ],
    );
  }
}
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
195

You don't have to define a separate variable in the widget scope, just do it inline:

TextField(
  controller: TextEditingController()..text = 'Your initial value',
  onChanged: (text) => {},
)
vovahost
  • 34,185
  • 17
  • 113
  • 116
  • 3
    What is meant by the double dots in "TextEditingController()..text"? – Ray Li Aug 16 '19 at 15:06
  • 21
    @RayLi It's called cascade operator. For info and example check here https://stackoverflow.com/questions/53136945/what-does-that-2-dots-mean-what-is-the-difference-between-1-and-2 – vovahost Aug 16 '19 at 15:09
  • 4
    Thanks!. This perfect for dynamic creation of widgets from lists of objects :) – davaus Sep 17 '19 at 04:16
  • 11
    @vovahost it's missing the dispose part, unfortunately. – Chris Rutkowski May 29 '20 at 08:54
  • 2
    This might be bad because you should dispose of TextEditingController's in the widget's dispose() method. – Travis Reeder Jul 09 '20 at 21:54
  • I am using Provider to provide initial Value to TextEditingController using the cascade approach but whenever I change the user gender let's say from 'Male' to 'Female' using ChangeNotifierProvider The TextFormField Data is being reset to initial Value Provided to the Controllers, is There any Approach that I can use. – C.M Talha Mar 11 '22 at 20:18
  • 1
    Found the Solution in my case, declaring the TextEditingController to final was the Problem :) – C.M Talha Mar 11 '22 at 20:22
  • this is the only one working for me. TYSM! – Dennis Mar 13 '22 at 13:08
67

I've seen many ways of doing this on here. However I think this is a little more efficient or at least concise than the other answers.

TextField(
   controller: TextEditingController(text: "Initial Text here"),
)
Kent
  • 2,343
  • 13
  • 21
47

If you are using TextEditingController then set the text to it, like below

TextEditingController _controller = new TextEditingController();


_controller.text = 'your initial text';

final your_text_name = TextFormField(
      autofocus: false,
      controller: _controller,
      decoration: InputDecoration(
        hintText: 'Hint Value',
      ),
    );

and if you are not using any TextEditingController then you can directly use initialValue like below

final last_name = TextFormField(
      autofocus: false,
      initialValue: 'your initial text',
      decoration: InputDecoration(
        hintText: 'Last Name',
      ),
    );

For more reference TextEditingController

Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24
  • I'm not sure if it's a good idea to set the initial value to the `text` property as per docs of `TextEditingController.text` property: Setting this will notify all the listeners of this TextEditingController that they need to update (it calls notifyListeners). For this reason, this value should only be set between frames, e.g. in response to user actions, not during the build, layout, or paint phases. (https://api.flutter.dev/flutter/widgets/TextEditingController/text.html) – Kirill Karmazin Aug 22 '19 at 20:30
36

This can be achieved using TextEditingController.

To have an initial value you can add

TextEditingController _controller = TextEditingController(text: 'initial value');

or

If you are using TextFormField you have a initialValue property there. Which basically provides this initialValue to the widget automatically.

TextFormField(
  initialValue: 'initial value'
)

To clear the text you can use _controller.clear() method.

Debasmita Sarkar
  • 6,367
  • 1
  • 9
  • 9
14

If you want to handle multiple TextInputs as asked by @MRT in the comment to the accepted answer, you can create a function that takes an initial value and returns a TextEditingController like this:

initialValue(val) {
  return TextEditingController(text: val);
}

Then, set this function as the controller for the TextInput and supply its initial value there like this:

controller: initialValue('Some initial value here....')

You can repeat this for the other TextInputs.

Muaad
  • 366
  • 2
  • 8
10
class _YourClassState extends State<YourClass> {
  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.text = 'Your message';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: TextFormField(
        controller: _controller,
        decoration: InputDecoration(labelText: 'Send message...'),
      ),
    );
  }
}
8
TextEdittingController _controller = new TextEdittingController(text: "your Text");

or

@override
  void initState() {
    super.initState();
    _Controller.text = "Your Text";
    }
Amir Ali Aghamali
  • 582
  • 1
  • 7
  • 23
5

inside class,

  final usernameController = TextEditingController(text: 'bhanuka');

TextField,

   child: new TextField(
        controller: usernameController,
    ...
)
BIS Tech
  • 17,000
  • 12
  • 99
  • 148
5

Easy and Efficient way

Assign controller to your TextFormField or TextField and in initState you can initialise it to the initial value like this

_controller = TextEditingController(text: 'Your initial value');
Hemal Moradiya
  • 1,715
  • 1
  • 6
  • 26
Pawan Meena
  • 71
  • 1
  • 3
5

Since none of the answers mention it, the TextEditingController should be disposed off after use. As in:

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  final myController = TextEditingController(text: "Initial value");

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: myController,
    );
  }

  @override
  void dispose() {
    // dispose it here
    myController.dispose();
    super.dispose();
  }
}
Neeraj
  • 2,376
  • 2
  • 24
  • 41
5

If you use TextEditingController just use the line in your class.

TextEditingController txtController = TextEditingController(text: 'Initial value')

TextField(
  controller: txtController,
);

If you don't use TextEditingController just go for initialValue

TextFormField(
      initialValue: 'your initial text',
  );

Full code

class _TestScreen extends State<Test> {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController(text: 'Initial value');
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
       TextFormField(       // First method
          initialValue: "Initial text"
        ),
       TextField(       // Second method
          controller: _controller, // Controller has the initial value
        ),
        
      ],
    );
  }
}
Anand
  • 4,355
  • 2
  • 35
  • 45
3
  1. When you are Using TextEditingController

If you use TextEditingController, set its text field to the desired value

TextEditingController txtController = TextEditingController()..text = 'Your initial text value'; 
TextField( controller: txtController ..... ) 
  1. When you are Not Using TextEditingController

If you are not using the TextEditingContller, use the initialValue field directly from the TextField widget:

TextFormField( initialValue: "Your initial text value" )
Shirsh Shukla
  • 5,491
  • 3
  • 31
  • 44
2

As for the above-mentioned answers all are right, but one thing is missing that I wanna add to it is how to customize the style of this default text of TextFormField.

TextEditingController textController = TextEditingController(text: '4');

 TextFormField(
    controller: textController,
     // This style property will customize the default controller text
    style: TextStyle(
    color: Colors.white,
    fontSize: 18,
    fontWeight: FontWeight.bold,
   ),
    ),

For this scenario, I would prefer to use TextEditingController instead of initialValue, Because if you want to change it later or modify it, then textController will continuously listen for your TextFormField input changes. This is my personal view, let me know if I am wrong.

Abdur Rehman
  • 313
  • 2
  • 6
2

if you use text form field and pass data from previous page to next page text form field use this

class ProfilePage extends StatefulWidget {

  late final String fname;

    ProfilePage({required this.fname});

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  final _form = GlobalKey<FormState>();
  
  late var FullNameController = TextEditingController(text: widget.fname);
  
}
lepsch
  • 8,927
  • 5
  • 24
  • 44
  • What the `late` is for here? – Wiktor Sep 03 '22 at 10:01
  • 1
    This is just what I was looking for, where I passed a string between pages. @Wiktor the `late` modifier allows the field to be initialized later, after its declaration. In this case, it avoids the error `The instance member 'widget' can't be accessed in an initializer.` – penguinrob Nov 30 '22 at 03:42
2

you can use this code sample:

    TextFormField(
          controller: nameController?..text='Your initial value',
         
        ),
1

You can do all of the above things but, if you want the API to show your data when its's get loaded it shows up like profile page. so here's the code:

TextEditingController _nameController = TextEditingController(); // initialize the controller
 // when API gets the data, do this:
 _nameController.text = response.data.fullName; or _nameController.text = "Apoorv Pandey"

I hope it clears everything. Happy coding!

Apoorv Pandey
  • 2,118
  • 2
  • 11
  • 13
  • This works for me but can we do it with focus node or using _initValue also? – Rohan Aug 02 '21 at 05:33
  • I can't understand your question, sorry can you elaborate a bit more? – Apoorv Pandey Aug 02 '21 at 06:04
  • yes like i am using focus node and _initvalue={ name: ''} in a name formtextfield and I want my _initvalue['name'] to be prefilled by fetching the name from server so after using controller I got the name but I am asking can we do without controller also ? – Rohan Aug 02 '21 at 06:24
  • As my experience you can't do that without controller, but you can use a variable of String type and pass in the label text inside decoration of the text form field it will be prefilled, and when user click on it it will go up and will be empty – Apoorv Pandey Aug 02 '21 at 06:47
  • 1
    ok thanks earlier I was trying with initialValue and it didn't work out but I would go with the controller for the time being – Rohan Aug 02 '21 at 10:27
  • Yeah sure, I hope I was able to help you. Good luck! – Apoorv Pandey Aug 03 '21 at 11:16
0

The best way would be replacing TextField to TextFormField because, we can use the property initialValue and avoid the complexity to manage state.

Using TextEditingControlleris not optimal because it needs to recreate a instance of TextEditingController just to pass a initial value. Additionally, how can you handle if you need to customize more and require access the text controller in other places ? It would be so confused