Dinesh
Dinesh

Reputation: 1225

Flutter - 'initialValue == null || controller == null': is not true. error

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,
);
}

Upvotes: 109

Views: 59279

Answers (3)

user19955346
user19955346

Reputation: 11

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

Upvotes: 1

V-SHY
V-SHY

Reputation: 4125

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;

Upvotes: 4

CopsOnRoad
CopsOnRoad

Reputation: 267594

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

Upvotes: 249

Related Questions