简体   繁体   中英

How to set the value to a TextFormField in flutter

I am trying to set a value to the TextFormField in flutter .

But I couldn't find a way to do that.

this is how my widget looks like:

Widget _showHeadlineField() {
      return TextFormField(
        textInputAction: TextInputAction.next,
        onEditingComplete: () {
          FocusScope.of(context).requestFocus(_descriptionNode);
        },
        controller: _headlineController,

        validator: (headline) {
          if (headline == null || headline.isEmpty) {
            return "Headline cannot be empty";
          }
        },
        decoration: InputDecoration(
          labelText: "Headline",
          hintText: "Covid-19 new stats",
          border: OutlineInputBorder(),
          icon: Icon(Icons.add_box),

        ),
      );
    }

I even tried initialValue but doesn't work. Can someone help me?

You can use

_headlineController.text = 'Your text';

Or when you create controller:

 _headlineController = TextEditingController(text: 'Your text')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM