w461
w461

Reputation: 2698

flutter: in BLoC, event comparison fails, though using equatbale

I am just about getting my first BLoC based feature running (though searching for my issue here provided insight into more pitfalls to come). It is quite confusing with those different tutorials and documentations out there, referring to different BLoC releases or just using different approaches for the same problem.

I am currently stuck on comparing the event to map to the appropriate state in my _bloc file. Even though event shows the same value (event = LogicGraphsNextProblemRequested) in the debugger, if (event == LogicGraphsNextProblemRequested) provides false.

I am trigging this from within StateLessWidget with

  @override
  Widget build(BuildContext context) {
    BlocProvider.of<LogicGraphsPStmntBloc>(context)
        .add(LogicGraphsNextProblemRequested());

The events, bloc etc is defined as follows

// =========== BLOC ================

class LogicGraphsPStmntBloc extends Bloc<LogicGraphsPStmntEvent, LogicGraphsPStmntState> {
  LogicGraphsPStmntBloc({@required this.logicGraphsRepository})
      : super(LogicGraphsPStmntInProgress());
  final LogicGraphsRepository logicGraphsRepository; within mapEventToState
  int currentProblem;

  @override
  Stream<LogicGraphsPStmntState> mapEventToState(
      LogicGraphsPStmntEvent event) async* {
    if (event == LogicGraphsNextProblemRequested) {
      _mapLGPStmntNewProblemRequested(currentProblem);
    }
    else if (event == LogicGraphsPStmntLoadRequested) {
      _mapLGPStmntLoadRequested(currentProblem);
    }
  }
}

// =========== EVENTS ================

abstract class LogicGraphsPStmntEvent extends Equatable {
  const LogicGraphsPStmntEvent();

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

class LogicGraphsNextProblemRequested extends LogicGraphsPStmntEvent {}

class LogicGraphsPStmntLoadRequested extends LogicGraphsPStmntEvent {}

// =========== STATES ================

abstract class LogicGraphsPStmntState extends Equatable {
  const LogicGraphsPStmntState();
  @override
  List<Object> get props => [];
}

class LogicGraphsPStmntInProgress extends LogicGraphsPStmntState {
}

class LogicGraphsPStmntLoadSuccess extends LogicGraphsPStmntState {
  const LogicGraphsPStmntLoadSuccess([this.statements = const []]);
  final List<String> statements;

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

  @override
  String toString() => 'LogicGraphsPStmntLoadSuccess { statements: $statements }';
}

class LogicGraphsPStmntLoadFailure extends LogicGraphsPStmntState {
}

RELATED QUESTION: Is this the correct way to request the initial data feed for that page? When I open that screen, the app shall select a random problem from a pile and (first of all) show a list of statements for that problem. So I wonder, if the approach above will create an endless loop of requesting a new problem which automatically also leads to feeding the statements to that screen, and the new rendering for the returned statements asks again for a new problem.

Upvotes: 0

Views: 222

Answers (1)

Ruben R&#246;hner
Ruben R&#246;hner

Reputation: 601

Try replacing the '==' with 'is' when you are using the equatable package to check the type of a variable.

Like this:

@override
Stream<LogicGraphsPStmntState> mapEventToState(
    LogicGraphsPStmntEvent event) async* {
  if (event is LogicGraphsNextProblemRequested) {
    _mapLGPStmntNewProblemRequested(currentProblem);
  }
  else if (event is LogicGraphsPStmntLoadRequested) {
    _mapLGPStmntLoadRequested(currentProblem);
  }
}

Upvotes: 1

Related Questions