1

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.

LWB
  • 426
  • 1
  • 4
  • 16

2 Answers2

0

change it to this:

@override
  void initState() {
    super.initState();
   _titleController.text = newTask.title;
  }
.
.
.
.
.
//And in your TextFormField
controller: _titleController,
Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
0

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

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

Make it:

controller: _titleController
Pablito
  • 521
  • 4
  • 15