使用Flatter,如何在单独的登录页面小部件中显示AuthService类中捕获的Firebase Auth错误消息?

使用Flatter,如何在单独的登录页面小部件中显示AuthService类中捕获的Firebase Auth错误消息?,firebase,flutter,google-cloud-firestore,firebase-authentication,Firebase,Flutter,Google Cloud Firestore,Firebase Authentication,我有一个带有注册表单的注册页面,然后调用一个AuthService类来注册用户,返回一个映射的自定义用户类。我可以检查函数调用的结果是否不为null,从而将用户导航到主页,但我无法确定如何设置状态或类似设置,以便在我的注册页面中实际向用户显示Firebase Auth消息,因为try/catch块在我的Auth服务类中 这是我的缩写注册屏幕小部件: class RegistrationScreen extends StatefulWidget { @override _Registrat

我有一个带有注册表单的注册页面,然后调用一个AuthService类来注册用户,返回一个映射的自定义用户类。我可以检查函数调用的结果是否不为null,从而将用户导航到主页,但我无法确定如何设置状态或类似设置,以便在我的注册页面中实际向用户显示Firebase Auth消息,因为try/catch块在我的Auth服务类中

这是我的缩写注册屏幕小部件:

class RegistrationScreen extends StatefulWidget {
  @override
  _RegistrationScreenState createState() => _RegistrationScreenState();
}

class _RegistrationScreenState extends State<RegistrationScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //abbreviated...
                RoundedButton(
                  onPressed: () async {
                    if (_formKey.currentState.validate()) {
                      dynamic result = await _auth.registerWithEmailAndPassword(email, password, displayName);
                     if (result != null) {
                       Navigator.pushNamedAndRemoveUntil(context, Home.id, (_) => false);
                      } 
                     }
                  }
                }
  }
但是,如何在注册屏幕中使用PlatformException设置状态或类似设置来向用户显示e.消息


谢谢。

您可以创建这样的类

class Errors {
   static String show(String errorCode) {
     switch (errorCode) {
       case 'ERROR_EMAIL_ALREADY_IN_USE':
         return "This e-mail address is already in use, please use a different e-mail address.";

       case 'ERROR_INVALID_EMAIL':
         return "The email address is badly formatted.";

       case 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL':
         return "The e-mail address in your Facebook account has been registered in the system before. Please login by trying other methods with this e-mail address.";

       case 'ERROR_WRONG_PASSWORD':
         return "E-mail address or password is incorrect.";

       default:
         return "An error has occurred";
     }
   }
}
try { 
   AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
   FirebaseUser user = result.user;

   return _userFromFirebaseUser(user);
} catch(e) {
   print(Errors.show(e.code));   // On this line, call your class and show the error message.
} 
然后,当您得到PlatformException错误时,您可以像这样向用户显示一个警报对话框

class Errors {
   static String show(String errorCode) {
     switch (errorCode) {
       case 'ERROR_EMAIL_ALREADY_IN_USE':
         return "This e-mail address is already in use, please use a different e-mail address.";

       case 'ERROR_INVALID_EMAIL':
         return "The email address is badly formatted.";

       case 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL':
         return "The e-mail address in your Facebook account has been registered in the system before. Please login by trying other methods with this e-mail address.";

       case 'ERROR_WRONG_PASSWORD':
         return "E-mail address or password is incorrect.";

       default:
         return "An error has occurred";
     }
   }
}
try { 
   AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
   FirebaseUser user = result.user;

   return _userFromFirebaseUser(user);
} catch(e) {
   print(Errors.show(e.code));   // On this line, call your class and show the error message.
} 

对不起,你能澄清一下我该在哪里叫那个班吗?我不确定如何向用户显示消息-我在注册屏幕中使用Email和Password调用我的注册器,如果成功,我想触发我的导航器,如果不成功,则使用setState或类似方式显示错误。我不知道如何使用结果来区分返回的成功用户和返回的错误,因为错误是在单独的AuthService类中的单独文件中打印的。如果我尝试If(result==error),它没有定义,我仍然需要捕获成功的登录。好的,创建一个errors.dart文件并粘贴这些代码。还要删除AuthService上的try-catch,并在生成结果时选中try-catch。会有用的。在你的构建中只使用一次。如果出现问题,请使用AlertDialog执行捕获。我已将try/catch从auth.dart文件中删除,并将其移动到registration\u page.dart文件中,但PlatformException仍会在auth.dart服务文件中抛出,而不是在registration\u page.dart注册文件中抛出,因此它似乎不起作用。它应该能起作用。您是否喜欢“_auth.registerWithEmailAndPassword(email,password,displayName).catchError(onError){…}”缓慢的回复抱歉-是的,谢谢,最终解决了这个问题!谢谢你的建议。