flutter - 在ShowDialog的TextField上显示错误文本

标签 flutter error-handling textfield

我正在尝试建立一个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。如果您需要它:它是一个有状态的小部件。

最佳答案

对于1st 2 if条件,您正在设置错误消息,但未将各自的 bool(boolean) 标志设置为true。
例:

if (e.code == 'wrong-passwod') {
    setState(() {
      _passwordErrorText = 'Password not Correct';
    });
  }
在这里,在setState()函数中,您还需要设置_wrongPassword = true;

关于flutter - 在ShowDialog的TextField上显示错误文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65089618/

相关文章:

flutter - 'Router' 是从 'package:fluro/src/router.dart' 和 'package:flutter/src/widgets/router.dart' 导入的

flutter - 如何在 Flutter 的 DropDown 中显示 json 嵌套数组?

Haskell:为什么 Maybe 和 Either 类型用作 Monad 时表现不同?

c++ - 为什么添加两个空格会产生错误的输出?

javascript - 搜索输入字段以搜索 JavaScript 数组 - 如何验证空输入框

java - 滚动到焦点文本字段

flutter - Flutter/Dart如何将 map 分组为DateTime

ios - 如何处理展开函数返回以避免崩溃的应用程序

java - JTextField 未读取用户的输入

ios - Flutter ios 构建失败 Pods-Runner-frameworks.sh