ReplayBloc function onTransition() handles event _Undo instead of user defined event

40 views Asked by At

While trying out the class ReplayBloc I encountered a problem, when using the method onTransition. Instead of handling the events I have defined, the internal event _Undo is handled.

A simplified version is listed below:

import 'package:replay_bloc/replay_bloc.dart';
import 'package:equatable/equatable.dart';

/// Bloc state
final class CounterState extends Equatable {
  const CounterState(this.i);
  final int i;

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

final class CounterInitial extends CounterState {
  const CounterInitial() : super(0);
}

/// Bloc events
sealed class CounterEvent extends ReplayEvent {}

final class Increment extends CounterEvent {}

final class Decrement extends CounterEvent {}

final  class Rewind extends CounterEvent{}



/// Bloc logic
class CounterBloc extends ReplayBloc<CounterEvent, CounterState> {
  CounterBloc() : super(CounterInitial()) {
    on<CounterEvent>((event, emit) => switch (event) {
          Increment() => emit(CounterState(state.i + 1)),
          Decrement() => emit(CounterState(state.i - 1)),
          Rewind() => emit(_rewind()),
        });
  }

  CounterState _rewind() {
    undo();
    return state;
  }

  @override
  void onTransition(
      covariant Transition<ReplayEvent, CounterState> transition) {
    super.onTransition(transition);
    print(transition.event.runtimeType);
    
    if (transition.event is Rewind) {
      print('Doing something during rewind. '); // <-------- Never reached.
    }
  }
}

void main(List<String> args) {
  final bloc = CounterBloc();

  bloc.add(Increment());
  bloc.add(Decrement());
  bloc.add(Rewind());
}

The output of the program main is listed below:


$ dart main.dart
Increment
Decrement
_Undo

Why is the internal event _Undo listed but not Rewind? In a real case scenario, I tried to take some action after a Rewind event.

0

There are 0 answers