【问题标题】:How to Handle Firebase Auth exceptions on flutter如何在 flutter 上处理 Firebase Auth 异常
【发布时间】:2022-12-31 08:28:09
【问题描述】:

如果用户尝试使用现有的电子邮件注册,我无法处理错误

Future _createUser(Users user, String name, x) async {
    UserCredential result = await FirebaseAuth.instance
        .createUserWithEmailAndPassword(
            email: _emailController.text.trim(),
            password: _passwordController.text.trim());
    try {
      result;
    } on FirebaseAuthException catch (e) {
      if (e.code == 'firebase_auth/email-already-in-use') {
        final snackBarx = SnackBar(
          elevation: 0,
          behavior: SnackBarBehavior.floating,
          backgroundColor: Colors.transparent,
          content: AwesomeSnackbarContent(
            message: 'Error please log in again and try again',
            contentType: ContentType.failure,
          ),
        );
        ScaffoldMessenger.of(context)
          ..hideCurrentSnackBar()
          ..showSnackBar(snackBarx);

【问题讨论】:

    标签: flutter firebase firebase-authentication


    【解决方案1】:

    在我的应用程序中,我这样处理:

    try {
      result;
    } on FirebaseAuthException catch (error) {
      if (error.code == "wrong-password") {
    
        //Handle error from Wrong Password
    
      } else if (error.code == "user-not-found") {
    
        //Handle error User Not Found
    
      } else if (error.code == "invalid-email") {
        
        //Handle error from Invalid Email
    
      } else if (error.code == "too-many-requests") {
    
        //Handle error from Too Many Requests        
    
      } else if (error.code == "network-request-failed") {
        
        //Handle error from NetWork failure
    
      }
    }
    

    【讨论】: