[Flutter×Firebase] firebase_auth 오류 처리 배우기 (예외 처리,try-catch)
글의 내용
앱을 만들면 필요할 때 오답을 하려고 할 때가 있다.
이것은 상황에 따라 Dialog에 오류 처리를 표시하는 글입니다.
 
 오류 처리(예외 처리)는
프로그램의 처리에서 방해된 사건을 처리할 때 이 처리를 오류로 처리하는 처리를 말한다.예외 처리라고도 불린다.
이번에 사용한 예외 처리의 기본 (try-catch)
    try{
      ログインする
    }
    catch(e){
      print(e); //エラー内容が出力
      //出力例) パスワードが間違っています
    }
Dart의 예외 처리는 기본적으로try-catch문구이다.try가 실행하는 처리에서 오류가 발생했을 때catch가 지정한 처리를 실행합니다.
매개 변수 e에서try가 실패했을 때의 오류를 수신할 수 있습니다.
새 등록 화면 오류 처리
새 로그인 화면 코드
ElevatedButton(
              onPressed: () async {
                startLoading();
                try {
                  await signUp();
                } on FirebaseAuthException catch (e) {
                  if (e.code == 'email-already-in-use') {
                    _emailAlreadyInUseDialog(context);
                  }
		  if (e.code == 'weak-password') {
                    _weakPasswordDialog(context);
                  }
                }
              },
              child: Text(
                '登録',
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
  코드 해설
on FirebaseAuthException을 지정하면 e.code에서 발생하는 오류 코드를 얻을 수 있습니다.
오류에 따른 처리를 지정할 수 있습니다.
FirebaseAuthException에서 발생할 수 있는 오류 코드는 Firebase입니다.auth 문서에서 확인할 수 있습니다.
메서드
로그인 화면 오류 처리
로그인 화면 코드
ElevatedButton(
                      onPressed: () async {
                        startLoading();
                        try {
                          await login();
                        } on FirebaseAuthException catch (e) {
                          if (e.code == 'user-not-found') {
                            _userNotFoundDialog(context);
                          } else if (e.code == 'invalid-email') {
                            _invalidEmailDialog(context);
                          }
                        }
                        endLoading();
                      },
                      child: Text(
                        'ログイン',
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                    ),
 방법Dialog에서 오류 처리 표시
이번에는 Dialog에 오류 처리가 표시됩니다.
또한 SnackBar class 등을 사용하여 오류 처리를 표시할 수 있습니다!
새 등록 오류 처리 Dialog
void _emailAlreadyInUseDialog(BuildContext context) {
  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext dialogContext) {
      return AlertDialog(
        title: Text('指定したメールアドレスは登録済みです'),
        actions: <Widget>[
          TextButton(
            child: Text('OK'),
            onPressed: () {
              Navigator.pop(dialogContext);
            },
          ),
        ],
      );
    },
  );
}
 로그인 오류 처리 Dialogvoid _userNotFoundDialog(BuildContext context) {
  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext dialogContext) {
      return AlertDialog(
        title: Text('ユーザーが見つかりません'),
        actions: <Widget>[
          TextButton(
            child: Text('OK'),
            onPressed: () {
              Navigator.pop(dialogContext);
            },
          ),
        ],
      );
    },
  );
}
  끝맺다
어때요?
이번 보도는 초보 엔지니어, 앞으로 Flutter에서 업무를 전개할 사람들에게 도움이 될 보도입니다!
의견이 있으면 제 트위터에 연락 주세요.
Reference
이 문제에 관하여([Flutter×Firebase] firebase_auth 오류 처리 배우기 (예외 처리,try-catch)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/hikaru24/articles/7c5d49b0e877b9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)