2

I'm experiencing an issue, that my state isn't updating and I can't find the problem in my code.

Basically I'm updating my state like this :

Bloc

  @override
  WorkoutState get initialState => WorkoutsLoaded(
      workouts: ["Workout1", "Workout2", "Workout3"], selectedWorkoutIndex: 0);

  @override
  Stream<WorkoutState> mapEventToState(WorkoutEvent event) async* {
    if (event is UpdateWorkoutPageIndex) {
      yield* _mapUpdateWorkoutPageIndexToState(event);
    }

    if (event is IncreaseWorkoutPageIndex) {
      yield* _mapIncreaseWorkoutPageIndexToState(event);
    }

    if (event is DecreaseWorkoutPageIndex) {
      yield* _mapDecreaseWorkoutPageIndexToState(event);
    }
  }

  Stream<WorkoutState> _mapUpdateWorkoutPageIndexToState(
      UpdateWorkoutPageIndex event) async* {
    print("_mapUpdateWorkoutPageIndexToState : " + this.state.toString());
    yield WorkoutsLoaded(
        workouts: ["Workout0", "Workout2", "Workout3"],
        selectedWorkoutIndex: event.pageIndex);
    print("_mapUpdateWorkoutPageIndexToState End : " + this.state.toString());
  }

Event

class UpdateWorkoutPageIndex extends WorkoutEvent {
  final int pageIndex;

  const UpdateWorkoutPageIndex({@required this.pageIndex});

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

  @override
  String toString() => "UpdateWorkoutPageIndex { pageIndex: $pageIndex }";
}

State

class WorkoutsUninitialised extends WorkoutState {}

class WorkoutsLoaded extends WorkoutState {
  final List<String> workouts;
  final int selectedWorkoutIndex;

  const WorkoutsLoaded({
    this.workouts,
    this.selectedWorkoutIndex,
  });

  @override
  String toString() {
    return "WorkoutsLoaded : " + this.workouts.toString() + " - " + this.selectedWorkoutIndex.toString();
  }
}

The state doesn't change, even when the event.pageIndex is different.

I can provide further code if necessary, but currently I'm not sure what code could be needed to solve my issue.

1 Answer 1

6

if your WorkoutEvent Class extends equatable, Then you need to add this line

    List<Object> get props => [pageIndex]; 

to your UpdateWorkoutPageIndex class

The aim is that the [List] of props (properties) will be used to determine whether two [Equatables] are equal. Hence, state will be updated.

4
  • I added it, but that didn't solve the problem unfortunately. I updated the code in the question accordingly.
    – TobiasW
    Jan 11, 2020 at 13:27
  • 2
    I was missing the same within the state class. Added it and it works!
    – TobiasW
    Jan 11, 2020 at 14:10
  • This definitely did the trick for me to update the value. Is it bad that the state is changing a lot though? Isn't Bloc supposed to prevent state changes and update widgets? I have a timer onto a textfield that upates every second. I was hoping to have it update the text without rebuilding the state all the time. Any suggestions? Aug 29, 2020 at 16:48
  • The idea that you would update the UI without updating state is backwards. UI updates are expensive, state change is cheap. A major purpose of reactive frameworks like Flutter, React, Vue, et al, is to carefully monitor the state and only update what portions of the UI are necessary.
    – Brandon
    Aug 27, 2022 at 22:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.