Home > Software design >  Input problems of TextFormField in Flutter
Input problems of TextFormField in Flutter

Time:11-12

I need to update the values of the screen. When I pushed the button it has to set the values of the screen. It is doing but when I try to change one field and go to the next field, the first value changes back to the first value. Here is my code part:

class _AccountScreenState extends State<AccountScreen> {
  final api = ApiServices();
  var formKey = GlobalKey<FormState>();
  var emailController = TextEditingController();
  var pwdController = TextEditingController();
  var phoneController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    emailController.text = widget.account.email!;
    phoneController.text = widget.account.phoneNumber!;
...
 child: TextFormField(controller: emailController,
                      decoration: InputDecoration(prefixText: "Email: ",
                                                  prefixStyle: TextStyle(fontWeight: FontWeight.w600),
                                                  labelText: "E-mail",
                                                  border: OutlineInputBorder(borderSide: BorderSide.none),
                                                  fillColor: Colors.white,
                                                  filled: true),
                        ),
...

CodePudding user response:

You can remove your code from Build method because when you use setstate method all Build method Rebuild. so you can just put you code in initstate method.

also you can use late keyword.

late TextEditingController emailController;
  @override
  void initState() {
  emailController = TextEditingController(text: widget.account.email!);
    // TODO: implement initState
    super.initState();
  }
  • Related