颤振状态无法正确更新

时间:2020-01-10 16:43:04

标签: flutter dart 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());
  }

事件

class UpdateWorkoutPageIndex extends WorkoutEvent {
  final int pageIndex;

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

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

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

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();
  }
}

即使event.pageIndex不同,状态也不会改变。

如有必要,我可以提供进一步的代码,但是目前我不确定解决我的问题可能需要什么代码。

1 个答案:

答案 0 :(得分:4)

如果您的WorkoutEvent Class扩展了equatable, 然后,您需要添加此行

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

进入您的UpdateWorkoutPageIndex

目的是要使用道具(属性)的“列表”来确定两个[Equatables]是否相等。 因此,状态将被更新。