0

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()) {

        }
Ashim Bhadra
  • 268
  • 4
  • 14

2 Answers2

2

For side effects such as showing snackbars / dialogs or navigating to another screen, you have to use BlocListener, something like this:

  BlocListener<YourGoogleSigninBloc, YourGoogleSigninState>(
    listener: (context, state) {
      if(state is GoogleLoginErrorState){
        // show snackbar here
      }
    },
    child: YourWidget(),
  )

you can also use BlocConsumer instead of nesting BlocListeners and BlocBuilders like this:

BlocConsumer<YourGoogleSigninBloc, YourGoogleSigninState>(
  listener: (context, state) {
    if(state is GoogleLoginErrorState){
        // show snackbar here
    }
  },
  builder: (context, state) {
    return YourWidget();
  },
)
Adnan Alshami
  • 949
  • 1
  • 7
  • 22
1

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)
NirmalCode
  • 2,140
  • 1
  • 14
  • 19