flutter:在 BLoC 中,事件比较失败,尽管使用了 equatbale

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

我即将获得我的第一个基于 BLoC 的功能 运行(虽然在这里搜索我的问题可以深入了解更多陷阱)。那些不同的教程和文档让人很困惑,指的是不同的 BLoC 版本,或者只是对同一个问题使用不同的方法。

我目前正在比较事件以映射到我的 _bloc 文件中的适当状态。即使事件在调试器中显示相同的值 (event = LogicGraphsNextProblemRequested),if (event == LogicGraphsNextProblemRequested) 也会提供 false.

我在 StateLessWidget 中用

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

事件、bloc等定义如下

// =========== 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 {
}

相关问题: 这是为该页面请求初始数据馈送的正确方法吗?当我打开该屏幕时,该应用程序应 select 从一堆随机问题中选择一个并(首先)显示该问题的陈述列表。所以我想知道,上面的方法是否会创建一个无休止的循环来请求一个新问题,这也会自动导致将语句提供给该屏幕,并且返回语句的新呈现会再次请求一个新问题。

当您使用 equatable 包检查变量类型时,请尝试将“==”替换为 'is'。

像这样:

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