我即将开始运行我的第一个基于BLoC的功能(尽管在此处搜索我的问题可以洞悉更多的陷阱)。与那里的那些不同的教程和文档,指向不同的BLoC版本或只是针对同一问题使用不同的方法而感到困惑。
我目前坚持比较该事件以映射到_bloc文件中的相应状态。即使事件在调试器中显示相同的值(event = LogicGraphsNextProblemRequested
),if (event == LogicGraphsNextProblemRequested)
也会提供false
。
我正在StateLessWidget内部使用
@override
Widget build(BuildContext context) {
BlocProvider.of<LogicGraphsPStmntBloc>(context)
.add(LogicGraphsNextProblemRequested());
事件,集团等定义如下
// =========== 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 {
}
相关问题: 这是请求该页面初始数据提要的正确方法吗?当我打开该屏幕时,应用程序应从一堆中选择一个随机问题,并(首先)显示该问题的声明列表。因此,我想知道,上述方法是否会导致请求新问题的无休止循环,并自动导致将语句馈送到该屏幕,并且返回的语句的新呈现又会询问新问题。
答案 0 :(得分:1)
在使用equatable包检查变量类型时,尝试用'is'替换'=='。
赞:
@override
Stream<LogicGraphsPStmntState> mapEventToState(
LogicGraphsPStmntEvent event) async* {
if (event is LogicGraphsNextProblemRequested) {
_mapLGPStmntNewProblemRequested(currentProblem);
}
else if (event is LogicGraphsPStmntLoadRequested) {
_mapLGPStmntLoadRequested(currentProblem);
}
}