简体   繁体   中英

in textformField how to use initialValue and controller both at same time?

i want to use controller and initialValue both at same time but showing error

TextFormField(
  controller: txtEmail,
  initialValue: initialValues['emailAddress'],
  decoration: InputDecoration(
    prefixIcon: Icon(Icons.email),
    label: Text('Email Address'),
    focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: accentColor)),
   enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: iconColor))
  ),
),

because text form field want assert(initialValue == null || controller == null). so you can set initialValue text into controller

You cant use the controller and initial value at the same time. It's either you use the initialValue with onChanged property or use the controller. If you need the controller and initial value, then you can assign your initial value to the controller.text

 final textController = TextEditingController(); void initState(){ textController.text = 'your initial value'; super.initState(); }

Add this:

txtEmail.text = initialValues['emailAddress'];

Remove initialValue:

TextFormField(
  controller: txtEmail,
  decoration: InputDecoration(
    prefixIcon: Icon(Icons.email),
    label: Text('Email Address'),
    focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: accentColor)),
   enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: iconColor))
  ),
),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM