Search code examples
firebaseflutterexceptionthrow

Flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Instance of 'CustomException'


I am trying to perform mobile authentication using firebase auth. In the signup process before performing signup I am checking whether the mobile number already exists if exists I am throwing a custom exception.

code

class CustomException implements Exception {
  String cause;
  CustomException(this.cause);
}

Future mobileAuth(
      String number, BuildContext context, Customer newCustomer) async {
    try {
      _databaseService
          .customerStream(customer: newCustomer)
          .listen((event) async {

        //Throwing custom exception if user exists
        if (event.length > 0) {
          throw CustomException("got error user");
        }

        await _firebaseAuth.verifyPhoneNumber(
          phoneNumber: number,
          verificationCompleted: (PhoneAuthCredential credential) async {
            await FirebaseAuth.instance
                .signInWithCredential(credential)
                .then((value) async {
              if (value.user != null) {
                Navigator.of(context).pushReplacement(MaterialPageRoute(
                    builder: (context) => HomeScreen(index: 3)));
              }
            });
          },
          verificationFailed: (FirebaseAuthException e) {
            if (e.code == 'invalid-phone-number') {
              print('The provided phone number is not valid.');
            }

            Navigator.of(context)
                .push(MaterialPageRoute(builder: (context) => MobileError()));
          },
          codeSent: (String verificationId, int? resendToken) {
            Navigator.of(context).push(MaterialPageRoute(
                builder: (context) => SignupForm3(
                      phoneNumber: number,
                      verificationCode: verificationId,
                      newCustomer: newCustomer,
                    )));

            
          },
          codeAutoRetrievalTimeout: (String verificationId) {
          },
        );
      });
      return true;
    } on CustomException catch (e) {
      print("User already exists ");
      print(e.cause);
      return false;
    } on FirebaseAuthException catch (e) {
      return false;
    } on PlatformException catch (e) {
      print(e.message);
      return false;
    } catch (e) {
      print(e.toString());
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => MobileError2()));
      return false;
    }
  }

I am getting this unhandled exception error. But I have implemented the catch to handle this custom exception.

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Instance of 'CustomException'


Solution

  • The Exception is not getting caught in the listener. So you need to add a try-catch in the listener callback:

    _databaseService
              .customerStream(customer: newCustomer)
              .listen((event) async {
              
                try {
                  //Listener code
                } on CustomException catch (e) {
                  //Handle CustomException
                } catch (e) {
                  //Handle other Exceptions
                }
              }