簡體   English   中英

使用 Flutter,如何在單獨的登錄頁面小部件中顯示在 AuthService 類中捕獲的 Firebase Auth 錯誤消息?

[英]With Flutter, how can I display Firebase Auth error messages caught within an AuthService class within a separate log in page widget?

我有一個帶有我的注冊表單的注冊頁面,然后是一個我調用的 AuthService 類來注冊用戶,返回一個映射的自定義 User 類。 我可以檢查函數調用的結果是否不為空,因此將我的用戶導航到主頁,但我無法弄清楚如何在我的注冊頁面中實際向用戶顯示 Firebase Auth 消息,如try/catch 塊在我的身份驗證服務類中。

這是我的縮寫注冊屏幕小部件:

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);
                      } 
                     }
                  }
                }
  }

registerWithEmailAndPassword 位於導入的 AuthService 類 auth.dart 中:

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final _firestore = Firestore.instance;
  //create User object
  User _userFromFirebaseUser(FirebaseUser user) {
    return user != null ? User(uid: user.uid, displayName: user.displayName) : null;
  }

  //auth change user stream
  Stream<User> get user {
    return _auth.onAuthStateChanged
    .map(_userFromFirebaseUser);
  }

  // register
  Future registerWithEmailAndPassword(String email, String password, String displayName) async {
                      try { 
                      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
                      FirebaseUser user = result.user;

                      return _userFromFirebaseUser(user);
                    } catch(e) {
                      print(e);
                    } 
                      } 
  }

如果我然后用格式錯誤的電子郵件對此進行測試,我會正確地打印到控制台:

flutter: PlatformException(ERROR_INVALID_EMAIL, The email address is badly formatted., null)

但是,如何在我的注冊屏幕中使用該 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";
     }
   }
}

然后,當您收到 PlatformException 錯誤時,您可以像這樣向用戶顯示警告對話框;

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.
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM