扑-如何将textformfield提交给注册按钮?

问题描述

我有4个textformfield。一个用于电子邮件用户名和两个密码字段。他们都有适当的验证。除了密码字段,我想检查两个密码是否相同。不知道该怎么做。我也有一个注册按钮 我想从那4个文本格式字段中收集输入,并将其发送到注册按钮,以使用Firebase注册一个新帐户。我如何获得我的注册按钮来接受那些表单输出

screenshot of the form

解决方法

您可以将控制器添加到TextField的四个中。

final _dataController = TextEditingController();

单击提交按钮时,您可以通过此方法获取值

String data = _dataController.text;
,

要检查密码和确认密码是否相同,您可能必须在表单验证中添加条件部分。

您可以像这样注册用户:

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

有关更多信息,请参见this

,

您可以使用分配给每个textformfield的变量作为value属性,并使用textformfield中现有的onchanged方法为它们分配新值。像这样:

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

或将控制器用于textformfield并在提交时获取其值:

TextFormField(控制器:_controller)

现在,在按钮上,您可以执行功能或调用具有该功能的方法:

RaisedButton(onPressed:(){///从 附加到textformfield的控制器字符串password = _controller.text;

//// Firebase身份验证代码在这里。 })