首页 > 解决方案 > 我无法正确验证 Firebase 用户并在 Flutter 中显示错误消息

问题描述

我可以很好地对用户进行身份验证,但是,当我故意输入一个不存在的电子邮件地址或正确的电子邮件地址但密码错误时,身份验证错误消息似乎有延迟。

我最初尝试使用 Future、async 和 await 在里面,但是按照 udemy 教程,他没有使用它,所以我把它拿走了,但它的运行方式没有区别。我正在做的与教程中不同的一件事是我也在更新云存储数据库。

loginUser(_email, _password) {
    formkey.currentState.save();
    if (formkey.currentState.validate()) {
      _auth
          .signInWithEmailAndPassword(email: _email, password: _password)
          .catchError((e) {
        Fluttertoast.showToast(
            msg: e.message,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.TOP,
            timeInSecForIos: 5,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
        );
      }).then((newUser) {
        var now = new DateTime.now();
        Firestore.instance
            .collection('users')
            .document(newUser.uid)
            .collection('userInfo')
            .document('userInfo')
            .setData({
          'Last login': now,
        })
            .then((onValue) {
          print('Created it in sub collection');
        }).catchError((e) {
          print('======Error======== ' + e);
        });
        getSignedInUser();
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => MyApp()),
        );
      });


    }
  }


  getSignedInUser() async {
    mCurrentUser = await _auth.currentUser();
    DocumentSnapshot result = await Firestore.instance.collection('users')
        .document(mCurrentUser.uid).collection('profile').document('profile')
        .get();
    String myResult = result['First Name'];
    Fluttertoast.showToast(
        msg: "Welcome $myResult!",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.TOP,
        timeInSecForIos: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }
}

我第一次输入无效的电子邮件时,我在控制台中收到错误,说明在 null 上调用 e.message 的行。如果我再次按登录,它会给我正确的错误消息。现在,如果我输入了正确的电子邮件地址,但密码错误(在同一个运行实例中)。它说用户名不存在。如果我再次单击它,它会给我正确的无效密码消息。任何想法我做错了什么?提前致谢

标签: firebaseauthenticationdartflutter

解决方案


推荐阅读