【问题标题】:Showing Error Text on TextField in ShowDialog在 ShowDialog 的 TextField 上显示错误文本
【发布时间】:2020-12-01 11:25:30
【问题描述】:

我正在尝试构建一个 AlertDialog 以在删除帐户之前重新验证用户身份。当重新验证不起作用时,我想显示错误文本。 正如我通过使用 print() 语句所看到的,错误得到了正确处理。但是,我无法让 TextField 将错误显示为对用户的反馈。我认为这一定与 SimpleDialog 本身的状态有关,而不是外部小部件的状态... 代码如下:


Future _validate({String email, String password}) async {
    setState(() {
      _wrongEmail = false;
      _wrongPassword = false;
      _emailErrorText = null;
      _passwordErrorText = null;
    });

    try {
      if (await userDataBase.checkCorrectEmail(
          email: email, loggedInUser: widget.user.id)) {
        throw FirebaseAuthException(
            message: 'email is not correct', code: 'user-not-found');
      }
      await userDataBase.authenticateUser(email, password);
    } catch (e) {
      if (e.code == 'wrong-passwod') {
        setState(() {
          _passwordErrorText = 'Password not Correct';
        });
      } else if (e.code == 'user-not-found') {
        setState(() {
          _emailErrorText = 'Email not Correct';
        });
      } else {
        setState(() {
          _emailErrorText = 'Email not Correct';
          _passwordErrorText = 'Password not Correct';

          _wrongPassword = true;
          _wrongEmail = true;
        });
      }
      throw e;
    }
  }

  Future _authenticate({String email, String password}) async {
    try {
      await _validate(email: email, password: password);
    } catch (e) {
      print(e.code);
      print(_wrongEmail);
      print(_emailErrorText);
      return;
    }

    print('correct behaviour');

    // Navigator.pop(context);
    // setState(() {
    //   _loading = true;
    // });

    // await userDataBase.deleteUser(widget.user.id);
    // Navigator.of(context).popUntil((route) => route.isFirst);
  }

  Future _confirmIdentity() {
    final emailController = TextEditingController();
    final passwordController = TextEditingController();
    _emailErrorText = null;
    _passwordErrorText = null;
    return showDialog(
      context: context,
      builder: (context) => AlertDialog(
        content: Container(
          height: 200,
          child: Column(
            children: [
              Text('Confirm Your Identity'),
              TextField(
                controller: emailController,
                decoration: InputDecoration(
                    errorText: _wrongEmail ? _emailErrorText : null,
                    labelText: 'email'),
              ),
              TextField(
                controller: passwordController,
                decoration: 
                     InputDecoration(
                       errorText: _wrongPassword ? _passwordErrorText : null, 
                       labelText: 'password'
                     ),
              ),
              Spacer(),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  FlatButton(
                    onPressed: () {
                      _authenticate(
                          email: emailController.text,
                          password: passwordController.text);
                    },
                    child: Text('Confirm'),
                  ),
                  FlatButton(
                    onPressed: () => Navigator.pop(context),
                    child: Text('Cancel'),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

谢谢。我可以发布所有小部件代码 ecc。如果你需要它:它是一个有状态的小部件。

【问题讨论】:

    标签: flutter error-handling textfield


    【解决方案1】:

    对于 1st 2 if 条件,您正在设置错误消息,但未将相应的布尔标志设置为 true。 示例:

    if (e.code == 'wrong-passwod') {
        setState(() {
          _passwordErrorText = 'Password not Correct';
        });
      }
    

    这里,在setState()函数中,您还需要设置_wrongPassword = true;

    【讨论】:

      【解决方案2】:

      我解决了这个问题。布尔变量_wrongPassword、_wrongEmail 是不必要的,当_PasswordErrorText 或_EmailErrorText 为空时,默认不显示错误文本。

      有必要构建一个有状态的小部件,它是 ShowDialog 的内容!然后一切正常。

      【讨论】:

        猜你喜欢
        • 2021-07-01
        • 2020-08-17
        • 2020-04-29
        • 2017-12-17
        • 2013-08-24
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        • 2019-11-16
        相关资源
        最近更新 更多