Mohamed Amin
Mohamed Amin

Reputation: 1067

controller and initial value in TextFormField do not work

I use TextFormField to get stored value from Firestore and display it to user in it's (edit profile) page,with ability to change field value.

My question is: how can i enable initial value(to get stored value) and controller to pick entered value by the user.

I read the document and i know i can't use both together,but is there an idea to allow textfield to get stored value and edit it in the same time?

            TextFormField(
                            controller: phone_controller,
                            keyboardType: TextInputType.number,
                            decoration:   InputDecoration(
                              icon: const Icon(Icons.phone),
                              hintText: phone,
                            //  labelText: phone,
                            floatingLabelBehavior: FloatingLabelBehavior.never,
                            ),
                            validator: (value) {
                              String pattern = r'(^(?:[+0]9)?[0-9]{10,12}$)';
                              RegExp regExp = new RegExp(pattern);
                              if (value.isEmpty || !regExp.hasMatch(value) ) {
                                return 'Please enter valid phone number like 01001234567';
                              }
                              return null;
                            },
                          )

Upvotes: 0

Views: 852

Answers (1)

Peter Obiechina
Peter Obiechina

Reputation: 2835

In your initState, you can set the initial value of the controller.

@override
void initState() {
  super.initState();

  phone_controller.text = 'phone_no';
}

phone_controller now has a default value;

Upvotes: 2

Related Questions