user54517
user54517

Reputation: 2420

Flutter - TextField controller not updating after textfield changes

I'd like to update the value of TextEditingController.text property. Here's my code:

_addContact(){
    Contact contact = new Contact();
    buildShowRoundedModalBottomSheet(
      context, 
      'Add contact', 
      editContact(contact),
      'contact',
      () => _updateContactList(contact)
    );
}

What happens here : First I create the contact. Then I call the bottom sheet, that render., giving it the content of the bottom sheet (a TextField, returned by editContact). So then in the emulator I edit the fields of contact (that has a TextEditingController). When I come back from EditContact, my contact hasn't been edited. There are still the default values :/

editContact(Contact contact){
  return Padding(
      padding: const EdgeInsets.only(top: 12.0),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                width: MediaQuery.of(context).size.width * 0.9,
                child: TextField(
                  controller: contact._contactRelationController,
                  decoration: InputDecoration(
                    hintText: contact._contactRelationController.text,
                  ),
                ),
              ), 
            ],
          ),
       ]
     )
  )
}

I thought that the controller of a textfield would be updated everytime its text property changes but its seems that it's not the case

Upvotes: 1

Views: 3722

Answers (2)

user54517
user54517

Reputation: 2420

Okay I resolved the problem. I just forgot to manually update the Contact object.

_addContact(){
    Contact contact = new Contact();
    buildShowRoundedModalBottomSheet(
      context, 
      'Add contact', 
      editContact(contact),
      'contact',
      (){
        contact.name = contact._contactNameController.text;
        _updateContactList(contact);
      } 
    );

  }

Shame on me. Hope it will help others :D

Upvotes: 1

KeeyPee
KeeyPee

Reputation: 310

Maybe try update the text in the setState method.

setState(() {
})

Upvotes: 2

Related Questions