How to change the set value of a text field

703 views Asked by At

So I have TextFormField and it has a set value. However when I try to change the value of it by typing in the Feild, it just reverts to the specified value. This is my code:

TextFormField(
                onEditingComplete: () {

                  FocusScope.of(context).unfocus();
                  setState(() {
                    
                    // THIS IS WHERE I NEED TO CHANGE ITS VALUE

                  });

                },
                
                controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
                cursorHeight: 23.0,
                decoration: InputDecoration(
                  disabledBorder: UnderlineInputBorder(),
                  border: OutlineInputBorder(),
                  hintText: "Enter Text",
                  labelText: "Title",
                  labelStyle: TextStyle(
                    fontSize: 20.0
                  ),
                ),
              ),

I need to somehow set _titleController.text to the value that is in the field when enter or the tick button is clicked - but I cant just do _titleController.text = _titleController.text; for obvious reasons. Please help. Comment if you need more details.

2

There are 2 answers

0
Pablito On BEST ANSWER

You assign the value of newTask.title to _titleController.text

controller: _titleController..text = newTask.title, //newTask IS AN OBJECT

Make it:

controller: _titleController
0
Huthaifa Muayyad On

change it to this:

@override
  void initState() {
    super.initState();
   _titleController.text = newTask.title;
  }
.
.
.
.
.
//And in your TextFormField
controller: _titleController,