1

I am building a sign in functionality using bloc pattern, if the entered credentials are invalid, bloc will return a authErrorState, so I will display a invalid credentials popup as soon as the bloc return a authError State

please check the code :

if (state is IsAuthLoadingState) {
              return const LoadingSpinnerWidget();
            } else if (state is IsAuthenticatedState) {
              WidgetsBinding.instance.addPostFrameCallback((_) {
                stopTimer();
                BlocProvider.of<AuthBloc>(context).add(LoadAuthStatus());
                Navigator.pop(context, true);
              });
            } else if (state is AuthErrorState) {
              WidgetsBinding.instance.addPostFrameCallback((_) {
                stopTimer();
                showCustomPopUp(state.message);

              });
            }

Bloc code :

  void _onLoginUser(LoginUser event, Emitter<AuthState> emit) async {
    emit(IsAuthLoadingState());
    final UserLoggedInResponse userDetails =
        await authRepository.handleLoginUser(event.phoneNumber, event.otp);
    if (userDetails.status == "success") {
      for (var item in userDetails.wishlist) {
        await _localRepo.addWishlistItem(item);
      }
      for (var item in userDetails.cart) {
        await _localRepo.addCartItem(item);
      }
      for (var item in userDetails.recentSearches) {
        await _localRepo.addRecentSearchTerm(item);
      }
      await _localRepo.addPurchasedItems(userDetails.purchasedItemIds);
      await _localRepo.setIsAuthenticated(
          userDetails.accessToken, userDetails.userId);
      emit(IsAuthenticatedState());
    } else {
      emit(AuthErrorState(
          message: userDetails.message, content: userDetails.content));
    }
  }

But, the invalid credentials popup written in authErrorState is getting called multiple times.

Any help is really appreciated. Thank you.

4
  • Your else if (state is AuthErrorState) { called multiple times, not WidgetsBinding. Add print statement inside this else statement and confirm it Commented May 18, 2022 at 4:26
  • @BISTech Thank you very much for taking time to help me out. You are right, code in else if block is getting executed multiple times. what should I do to execute it only once. I added my bloc code as well, Please check once. Commented May 18, 2022 at 10:05
  • can you add another print statement above AuthErrorState in bloc? Commented May 18, 2022 at 11:33
  • @BISTech, I tried adding it. print statement above AuthErrorState in bloc is getting printed only once. and print statement in else if block in UI page is getting printed multiple times. Commented May 18, 2022 at 14:24

1 Answer 1

1

As I didn't found any alternative options, I someone tried to manage this for now like this,

I used a bool variable called isErrorShown, and it was set to false by default,

once the code in widgetsBinding is executed, it will set the isErrorShown to true, function is widgetsBinding checks the value of isErrorShown and executes only if it is false :

else if (state is AuthErrorState) {
              print("error state");
              WidgetsBinding.instance.addPostFrameCallback((_) {
                if (!isErrorShown) {
                  stopTimer();
                  if (state.message ==
                      "user does not exits, please create user") {
                    Navigator.pushReplacementNamed(context, '/create-user',
                        arguments: CreateUserPage(
                          showProfile: widget.showProfile,
                          phoneNumber: phoneNumberController.text,
                          otp: otpController.text,
                        ));
                    // BlocProvider.of<AuthBloc>(context).add(LoadAuthStatus());
                    // Navigator.pushNamed(context, '/create-user');
                  } else {
                    showCustomPopUp(state.message);
                  }
                  isErrorShown = true;
                }
              });
Sign up to request clarification or add additional context in comments.

1 Comment

This bool variable also works for my case. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.