【问题标题】:How to show alert on firebase auth errors flutter如何在firebase auth错误上显示警报颤动
【发布时间】:2021-03-19 06:04:12
【问题描述】:

我想在 Firebase 身份验证中出现错误时显示警报对话框。 Firebase 已经在 UI 中打印了错误,但我想向用户显示一个对话框。

这是我的 createUser 和 signInUser 功能以及我的注册按钮功能

  Future registerWithEmailAndPassword({String email,password,username,image,phoneNumber}) async {
    try {
      UserCredential userCredential = await _firebaseAuth
          .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);
    }
  }
 Future signInWithEmailAndPassword({String email, String password}) async {
    try {
      UserCredential userCredential = await _firebaseAuth
          .signInWithEmailAndPassword(
          email: email,
          password: password
      );
      User user = userCredential.user;
      assert(user.uid != null);
      email = user.email;
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-not-found') {
        print('No user found for that email.');
      } else if (e.code == 'wrong-password') {
        print('Wrong password provided for that user.');
      }
    }
  }
            press: () {
              if (formKey.currentState.validate()) {
                formKey.currentState.save();
                    context
                    .read<Authentication>()
                    .signInWithEmailAndPassword(
                    email: emailController.text,
                    password: passwordController.text)
                    .whenComplete(() => Navigator.pushReplacement(
                    context,
                    MaterialPageRoute(
                        builder: (context) =>
                            HomeScreen())));
              }
            },

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    您可以设置与此类似的AlertDialog 小部件。在您的情况下,Yes/No 按钮可能是多余的,如果是这样,只需转换为 ok 按钮,然后您就不必检查返回结果。

    Future<String> showYesNoAlertDialog({
      @required BuildContext context,
      @required String titleText,
      @required String messageText,
    }) async {
      // set up the buttons
      final Widget yesButton = FlatButton(
        onPressed: () => Navigator.pop(context, 'yes'),
        child: const Text('Yes'),
      );
      final Widget noButton = FlatButton(
        onPressed: () => Navigator.pop(context, 'no'),
        child: const Text('No'),
      );
    
      // set up the AlertDialog
      final alert = AlertDialog(
        title: Text(titleText),
        content: Text(messageText),
        actions: [
          yesButton,
          noButton,
        ],
      );
    
      // show the dialog
      return showDialog(
        context: context,
        builder: (context) => alert,
      );
    }
    

    然后你的打印语句输出错误,你会像这样调用上面的小部件

    final dr = await showYesNoAlertDialog(
      context: context,
      titleText: 'Authentication Error',
      messageText:
          'There has been an error during authentication.  Would you like to retry?',
    );
    
    if (dr == 'yes') {
      // Yes button clicked
    }
    else {
      // No button clicked
    }
    

    【讨论】:

    • 非常感谢。我还有一个问题“我的应用程序即使在 Firebase 身份验证中出现错误时也会导航到下一个屏幕”我该如何阻止它?
    • @PetersVictor whenComplete() 方法定义了 The [action] function is called when this future completes, whether it does so with a value or with an error. 所以即使它成功或错误登录也将始终被调用。对于您的方法 signInWithEmailAndPassword() 完成后的情况,它将始终根据您的代码
    • 谢谢Reign,那么在进入下一页之前我将如何确保登录或注册成功
    猜你喜欢
    • 2020-08-26
    • 2021-04-22
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多