简体   繁体   中英

flutter - how to submit textformfield to a sign up button?

I have 4 textformfield. One for email, username, and 2 password fields. They all have proper validation. Except for the password field where I want to check if the 2 passwords are the same. Not sure how to do that part. I also have a register button I want to gather input from those 4 textform field and send it to the register button to sign up a new account using firebase. How do i get my register button to accept those form outputs?

screenshot of the form

You can add controller to four of your TextField .

final _dataController = TextEditingController();

When submit button is clicked, you can get the value by this

String data = _dataController.text;

For checking if both the password and confirm passwords are the same u may have to add a conditional part in the validation of the form.

You can register a user like this:

String _email;
String _password; //add these two to the validation and set the value state = to these fields respectively.

try {
         UserCredential userCredential = await 
         FirebaseAuth.instance.createUserWithEmailAndPassword(
         email: _email,
         password: _password
      );
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        print('The password provided is too weak.');
      } else if (e.code == 'email-already-in-use') {
        print('The account already exists for that email.');
      }
    } catch (e) {
      print(e);
    }

For more information refer this

you can use variable assigned to each textformfield as a value property and assign new value to them with onchanged method existing in textformfield. Something like this:

TextFormField( value: _password, onchange: (val) { _password = val; } )

Or use controller for textformfield and get their value on submit:

TextFormField( controller: _controller )

Now, on button you can execute your functionality or call a method which has the functionality:

RaisedButton ( onPressed: () { /// Getting data from the controller attached to textformfield String password = _controller.text;

//// Firebase auth code here. } )

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