【问题标题】:Flutter PlatformException invalid email formatFlutter PlatformException 无效的电子邮件格式
【发布时间】:2020-10-30 15:56:22
【问题描述】:

我正在调整我的应用程序,并添加了一个“全名”字段以存储在 firebase 中并显示在用户个人资料页面上。我在注册新用户时收到错误“PlatformException(ERROR_INVALID_EMAIL,电子邮件地址格式错误。,null)”。从其他类似的帖子中,我找不到任何让我遇到障碍的命名约定。

注册.dart:

                    SizedBox(height: 20.0),
                    TextFormField(
                        decoration:
                            textInputDecoration.copyWith(hintText: 'Full Name'),
                        validator: (val) =>
                            val.isEmpty ? 'Enter a Full Name' : null,
                        onChanged: (val) {
                          setState(() => fullname = val);
                        }),
                    SizedBox(height: 20.0),
                    TextFormField(
                        decoration:
                            textInputDecoration.copyWith(hintText: 'Email'),
                        validator: (val) =>
                            val.isEmpty ? 'Enter an email' : null,
                        onChanged: (val) {
                          setState(() => email = val);
                        }),
                    SizedBox(height: 20.0),
                    TextFormField(
                        decoration:
                            textInputDecoration.copyWith(hintText: 'Password'),
                        obscureText: true,
                        validator: (val) => val.length < 6
                            ? 'Enter a password 6+ chars long'
                            : null,
                        onChanged: (val) {
                          setState(() => password = val);
                        }),
                    SizedBox(height: 20.0),
                    RaisedButton(
                        color: Colors.blue[400],
                        child: Text(
                          'Register',
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () async {
                          if (_formKey.currentState.validate()) {
                            setState(() => loading = true);
                            dynamic result =
                                await _auth.registerWithEmailAndPassword(
                                    fullname, email, password);
                            if (result == null) {
                              setState(() {
                                error = 'please supply a valid email';
                                loading = false;
                              });
                            }
                          }
                        }),

auth.dart

Future registerWithEmailAndPassword(String email, String password, String fullname) async {
    try {
      AuthResult result = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);
      FirebaseUser user = result.user;
      return _userFromFirebaseUser(user);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

【问题讨论】:

    标签: firebase flutter authentication dart google-cloud-firestore


    【解决方案1】:

    你的函数声明说你的参数按这个顺序String email, String password, String fullname,但是你使用它们的方式是这个顺序fullname, email, password。改变

    await _auth.registerWithEmailAndPassword(fullname, email, password);
    

    await _auth.registerWithEmailAndPassword(email, password, fullname);
    

    唯一的问题是一个错字。

    您可以使用命名参数,这样在出现此类问题时可能会更明显,但我发现它仅对具有大量参数的函数或构造函数有用。

    【讨论】:

      【解决方案2】:

      您需要同时修剪电子邮件和密码

      【讨论】:

      • 请添加您的答案的代码 sn-p 示例
      猜你喜欢
      • 1970-01-01
      • 2020-08-01
      • 2012-05-28
      • 1970-01-01
      • 2022-08-21
      • 2020-04-15
      • 2020-04-20
      • 2014-11-14
      • 1970-01-01
      相关资源
      最近更新 更多