【发布时间】:2022-01-03 08:09:38
【问题描述】:
我是 Flutter 和 Bloc 的新手,我正在尝试实现一个约会应用程序,该应用程序涉及滑动用户喜欢/不喜欢。我正在使用 Bloc v8.0.1,如果用户滑动,我在 Bloc 类中遇到困难。我想从列表中删除用户。目前,我在emit(SwipeLoaded(users: List.from(state.props)..remove(event.user))); 代码行中收到错误'type 'List ' is not a subtype of type 'User''。我尝试了不同的方法来访问该列表,但我想不出一种可行的方法。我已经包含了我能想到的所有相关代码,任何帮助将不胜感激!
集团类
class SwipeBloc extends Bloc<SwipeEvent, SwipeState> {
SwipeBloc() : super(SwipeLoading()) {
on<SwipeEvent>(
(event, emit) async {
if (event is SwipeLeftEvent) {
if (state is SwipeLoaded) {
try {
emit(SwipeLoaded(users: List.from(state.props)..remove(event.user)));
} catch (_) { }
}
}
状态类
class SwipeLoading extends SwipeState {}
class SwipeLoaded extends SwipeState {
final List<User> users;
const SwipeLoaded({
required this.users,
});
@override
List<Object> get props => [users];
}
事件类
class SwipeLeftEvent extends SwipeEvent {
final User user;
SwipeLeftEvent({
required this.user,
});
@override
List<Object> get props => [user];
}
【问题讨论】: