Flutter-‘ initialValue = = null | | controller = = null’: 不正确

我试图为文本字段设置一个初始值。但是我不能在文本表单字段中设置初始值。我得到这个错误 'initialValue == null || controller == null': is not true

密码:

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

}

40901 次浏览

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

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;