110

I'm trying to set an initial value for the text field. But I Can't set the initial value in text form field. I'm getting this error 'initialValue == null || controller == null': is not true .

code:

Widget buildFirstName(BuildContext context) {
 valueBuilder = valueBuild();

return TextFormField(
  controller: firstNameController,
  initialValue: valueBuilder,
  decoration: InputDecoration(
    hintText: "Enter Name",
    fillColor: Colors.white,
    hintStyle: TextStyle(
        color: Color.fromRGBO(0, 0, 0, 1.0),
        fontFamily: "SFProText-Regular"),
  ),
  validator: validatingName,
);
}
4
  • 14
    You can't have both an initialValue and a controller Commented Oct 7, 2018 at 19:09
  • Then how to set initial value and how can I track the value entered by the user in the field Commented Oct 7, 2018 at 20:13
  • just using a TextEditingController :), but you can't use both (initialValue + controller) Commented Oct 7, 2018 at 23:58
  • you can set an initial value in your TextEditingController when its constructor is created Commented Dec 24, 2019 at 7:18

3 Answers 3

251

You can't use both initialValue and controller at the same time. So, it's better to use controller as you can set default text in its constructor.

Here is an example.

// Create the controller. 
final controller = TextEditingController(text: "Your initial value");

Widget build(BuildContext context) {
  return TextFormField(
    controller: controller, // Assign it here. 
    // ...
  );
}

To get the value entered by the user, use:

controller.text
Sign up to request clarification or add additional context in comments.

5 Comments

i am creating a form so that when user filling the form for the first time the text field's initial value has to be empty but once he completed creating the form and submitted it and when he tries to edit the details of him for eg. name then field should have initial value which the user entered previously
You have to Use OnSaved Method to save the value of the Form & then use it in edit mode.In the Init Method - firstNameController.text = ''; give it an Empty Value for the First Run & in Edit Mode give it firstNameController.text = 'Value'; You have to use two variables 1 Bool ( for Edit) 2 value (OnSaved Method).
You can use SharedPreferences for this case. Initially the default value is going to be null and when user saves something, you can save that data too and retrieve it later on and simply put that data with if else condition to work with controller.
Why is a controller required to update a control? It seems complex
This fixes it, thank you but the error is so vague and the named parameter "text" makes no sense to me. It should clearly be "initialValue" or something alike. Sad this is still so weird in 2024
4

You cannot have controller and initialValue for TextFormField at the same time which we know from @CopsOnRoad thread.

Besides of passing default text to the constructor of controller, you can modify the value of the TextFormField by assigning value to the controller text

firstNameController.text = valueBuilder;

Comments

1

controller..setText("Your initial value text")

2 Comments

Regardless that your answer is correct or not, you are encouraged to edit it to explain why this is the best answer to the question.
passwordController.text = "password"; working

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.