Tolga Yılmaz
Tolga Yılmaz

Reputation: 518

Flutter // I can't catch the Exceptions (Firebase app)

I have a big problem but I know it is small for humankind. I'm trying email and password Authentication on Firebase with Flutter. But I always get some exceptions. (PlatformException and FirebaseAuthExceptions). `

void signInUser(String email, String password) async {
    try {
      if (_auth.currentUser == null) {
        User _oturumAcanUser = (await _auth.signInWithEmailAndPassword(
                email: email, password: password))
            .user;
        Get.to(HomeScreen());
        Get.snackbar("Oturum açıldı", "message");
      } else {
        Get.snackbar("Oturum zaten açık", "message");
        Get.to(HomeScreen());
      }
    } on PlatformException catch (e) {
      _error = e.message;
      Get.snackbar("başlık", e.message);
      throw e;
    } catch (e) {
      _error = e.toString();
      Get.snackbar("başlık", e.message);
    }
  }

`

Also, I'm using the getX package. I tried everything but I can't catch the exceptions. When I call this method from UI, the SDK (VSCode) pausing debugging app and show exceptions in red alert.

I read a lot of documents about this situation but I couldn't found a solution.

Upvotes: 1

Views: 238

Answers (2)

Tolga Yılmaz
Tolga Yılmaz

Reputation: 518

void signInUser(String email, String password) async {
try {
  if (_auth.currentUser == null) {
    User _oturumAcanUser = (await _auth.signInWithEmailAndPassword(
            email: email, password: password))
        .user;
    Get.off(HomeScreen());
    Get.snackbar("Oturum açıldı", "message", backgroundColor: Colors.teal);
  } else {
  
    Get.off(HomeScreen());
  }
} on FirebaseAuthException catch (e) {
  _error = e.code;
  Get.snackbar("Giriş Yapılırken Hata",
      "Giriş ypaılırken hata ile karşılaşıldı. Kod: ${e.code}",
      snackPosition: SnackPosition.BOTTOM, snackStyle: SnackStyle.FLOATING);
  throw e;
} catch (e) {
  _error = e.toString();
  Get.snackbar("Hata",
      "Sunucu beklenmedik bir hata ile karşılaştı. Detaylı bilgi: ${e.message}");
}

I just did something like this and solve my problem. Still I dont understand where is different catch things. Thanks for all help. <3

Upvotes: 1

Akif
Akif

Reputation: 7640

Ok, you can try to use the catch method after signInWithEmailAndPassword like this:

void signInUser(String email, String password) async {
    try {
      if (_auth.currentUser == null) {
        User _oturumAcanUser = (await _auth.signInWithEmailAndPassword(
                email: email, password: password)
             // Here, you can handle the error!
             .catchError((error) { 
               print("The error message is: ${error.message}");
               Get.snackbar("başlık", error.message);
            })
            .user;
        Get.to(HomeScreen());
        Get.snackbar("Oturum açıldı", "message");
      } else {
        Get.snackbar("Oturum zaten açık", "message");
        Get.to(HomeScreen());
      }
    } on PlatformException catch (e) {
      _error = e.message;
      Get.snackbar("başlık", e.message);
      throw e;
    } catch (e) {
      _error = e.toString();
      Get.snackbar("başlık", e.message);
    }
  }

Upvotes: 0

Related Questions