Home > Enterprise >  Is there a way to use the results of a catch block inside a widget in Flutter
Is there a way to use the results of a catch block inside a widget in Flutter

Time:05-15

I am building a flutter app with Firebase as the back end. I have created an AuthService class on a separate file and import and use the Auth functions inside the log in screen.

This is my AuthService Class.

class AuthService {
  Future<UserModel?> signInWithEmailAndPassword(
      String email, String password) async {
    try {
      final cred = await _auth.signInWithEmailAndPassword(
          email: email, password: password);

      return _userFromFirebase(cred.user);
    } on auth.FirebaseAuthException catch (e) {
      print(e.toString());
      return null;
    }
  }
}

In the sign in page, I initialize the function:

    final auth = Provider.of<AuthService>(context);

Then use it in an onPressed :

                          press: () async {

                            // SIGN IN WITH EMAIL AND PASSWORD
                            dynamic result =
                                await auth.signInWithEmailAndPassword(
                                    email, password);

                            // IF SIGN IN FAILS
                            if (result == null) {
                              setState(() {
                                errorSigningIn = 'Sign in error';
                                //this is where I want to use the error response. 
                              });
                            }
                          },

I am stuck on using the error I catch in the signInWithEmailAndPassword function and assigning it to the errorSigningIn variable in the SignIn widget.

I am new to this, please help.

Thanks.

CodePudding user response:

You can create your own class to handle auth results. For example:

class AuthResult {
    final int code;
    final UserModel? user;
    final String? errorMessage;

    AuthResult(this.code, {
        this.user,
        this.errorMessage,
    });
}

This class can help you to handle all cases of logging in. And this is what you should do with your signing in method:

class AuthService {
  Future<AuthResult> signInWithEmailAndPassword(
      String email, String password) async {
    try {
      final cred = await _auth.signInWithEmailAndPassword(
          email: email, password: password);

      return AuthResult(200, user: _userFromFirebase(cred.user));
    } on auth.FirebaseAuthException catch (e) {
      print(e.toString());
      return AuthResult(0 /*<-- your error result code*/, e.toString());
    }
  }
}

And, finally, your onPressed:

                          press: () async {

                            // SIGN IN WITH EMAIL AND PASSWORD
                            AuthResult result =
                                await auth.signInWithEmailAndPassword(
                                    email, password);

                            // IF SIGN IN FAILS
                            if (result.code != 200) {
                              setState(() {
                                errorSigningIn = result.errorMessage; //<-- Get your error message
                                //this is where I want to use the error response. 
                              });
                            }
                          },
  • Related