flutterdartbloc

BlocBuilder builder function gets called only once


Good evening, I have been working with bloc pattern and I have some issues: The state update is being called only once on BlocBuilder, no matter what I do

What I have as state is:

class DateScreenState {
  Future<List<PrimaryPetModifierModel>> primaryPetModifiers;
  Future<List<SecondaryPetModifierModel>> secondaryPetModifiers;

  PrimaryPetModifierModel primaryPetModifierSelected;
  SecondaryPetModifierModel secondaryPetModifierSelected;

  Widget animatedWidget;

  DateTime dateOfSchedule;
  DateTime timeOfSchedule;

  bool shouldReload = false;
  bool isFirstCallAnimation = true;
}

And my mapEventToState looks like this:

@override
  Stream<DateScreenState> mapEventToState(
      ScheduleDateScreenEvent event) async* {
    if (event is SelectPrimaryModifierEvent) {
      state.primaryPetModifierSelected = event.modifier;
      yield state;
    } else if (event is SelectSecondaryModifierEvent) {
      state.secondaryPetModifierSelected = event.modifier;
      yield state;
    }
  }

My exact issue is, when I change a value in a DropdownButton, it will fire a SelectedPrimaryModifierEvent or SelectedSecondaryModifier event, the event firing works fine, but the state yielding and updating will happen only once after the first fire of any of those events, after that, BlocBuilder builder function will not be called anymore after any event.


Solution

  • You're yielding the same state with each event, despite the fact that you're changing a variable with the DateScreenState class. Try splitting up your primary and secondary into different state classes and yield them separately in your mapEventToState.