Flutter bloc – Show snackbar on state change

Issue

I am trying to log in with Google. When googleSignInAccount I yield new state with PlatformExecption. Now, How can I access that value in order to do some changes.

try{
    final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();

    if(googleSignInAccount == null){
     yield GoogleLoginErrorState(error: PlatformException(code: 'user-cancel'));
    }
}

my state.dart

class GoogleLoginErrorState extends GoogleLoginState {
  final PlatformException error;
  GoogleLoginErrorState({this.error});

  @override
  List<Object> get props => [error];
}

my BlocBuilder

if (state == GoogleLoginErrorState()) {

        }

Solution

In the bloc builder,

if (state is GoogleLoginErrorState) {

}

This checks if the state data type is GoogleLoginErrorState

And use the state

Text(state.error.code)

Answered By – NirmalCode

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *