1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Why isn't bloc builder recognizing the state change? If I wrap emit in a...

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 07:52.

  1. Stack

    Stack Membro Participativo

    I'm working on a flutter project and all of a sudden, the bloc builder I was using stopped recognizing a specific state change. Here is an example of the code

    class ProfileBloc extends HydratedBloc<ProfileEvent, ProfileState>
    ...

    on<ProfileSelected>(_mapProfileSelectedToState);

    ...

    FutureOr<void> _mapProfileSelectedToState(
    ProfileSelected event, Emitter<ProfileState> emit) async {
    emit(state.copyWith(
    profileSelectionStatus: ProfileSelectionStatus.selecting,
    ));

    setHeaders();
    setRedirect();

    emit(state.copyWith(
    selectedProfile: event.selectedProfile
    profileSelectionStatus: ProfileSelectionStatus.selected,
    ));
    }


    ...

    BlocBuilder<ProfileBloc,ProfileState>(builder: (context, profileState) {

    return profileState.profileSelectionStatus == ProfileSelectionStatus.selected ? Text("Profile Selected") : Text("Profile Not Selected");
    }

    ...


    when the BlocBuilder runs, it never registers the ProfileSelectionStatus.selected state. It always returns Profile Not Selected. That is until I refresh the browser, then it reports as Profile Selected. If I wrap the second emit(the profileSelected one) in the bloc in a Future.delayed(Duration.zero,()=>emit...), then the bloc builder works as expected returning Profile Selected after it's emitted.

    The thing is, it worked for a long time, then without changing those two blocs of code, it stopped working. Any ideas on what's wrong?

    Continue reading...

Compartilhe esta Página