1

I have the following component on my flutter page.

TextFormField(
                    controller: surveyDateController, 
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      hintText: 'Survey Date',
                    ),
                  ),

And i have the following controller for the above control

  final surveyDateController = TextEditingController();

The data is coming from _data.leadSurvey[0].title which is loaded from an API. Once the data is loaded I am calling the setState method as follows:

setState(() {
        _data = data;
      });

But the data in the TextFormField is not getting updated. Tried using the initialValue property but it generates a null error.

Any help guys?

5 Answers 5

2

Set data to the controller in the setState()

  setState(() {
        _data = data;
        surveyDateController.text = _data.leadSurvey[0].title
      });
Sign up to request clarification or add additional context in comments.

Comments

0

I am not completely sure where data should appear, but if you want it to appear in your TextFormField, I guess you have to call surveyDateController.value = data

5 Comments

Since there are around 20 TextFormFields in my page, is it recommended to set the value for every TextFormField using the controller? <controllername>.value=<value>
When I try to do that I am getting an error as following: A value of type String can not be assigned to a variable of TextEditingValue
@iShah I am not completely sure what your goal is. Normally a TextFormField is for inputting Text and not displaying it. Also, do you have just one TextEditingController for all of your Fields?
@iShah Oh, yeah, its like @Jhoan said surveyDateController.text. I had the native Android in my head.
What i am trying to do is to retrieve the data from the server (using API) and allowing the user to edit it and send it back to the server.
0

after you get the data String from the server

then set your editing controller (surverDateController)

surveyDateController.text = _data.leadSurvey[0].title

1 Comment

It works fine.. the only issue here is I have to keep doing this manually everytime the data is changed..
0

let's say you have $your_text_controller$ and have assigned it to your text field as follow:

TextFormField(
                    controller: surveyDateController, 
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      hintText: 'Survey Date',
                    ),
                  ),

now, you can set the text of this text field by calling this function everywhere you want:

void setText(){

    setState((){
        surveyDateController.text = "Your Text"; // Ex. _data.leadSurvey[0].title
    });
}

Comments

0

To give the TextFormField a default value:

final surveyDateController = TextEditingController(text: 'initial data here');

To re-assign the TextFormField a value:

surveyDateController.text = _data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.