Error: type 'List<User>' is not a subtype of type 'User' in Flutter, using Bloc 8.0.1

Error: type 'List<User>' is not a subtype of type 'User' in Flutter, using Bloc 8.0.1

我是 flutter 和 Bloc 的新手,我正在尝试实现一个约会应用程序,该应用程序涉及将用户滑动到 like/dislike。我正在使用 Bloc v8.0.1,如果用户滑动,我在 Bloc Class 中遇到困难。我想从列表中删除用户。目前,我在 emit(SwipeLoaded(users: List.from(state.props)..remove(event.user))); 代码行中收到错误消息“type 'List < User>' is not a subtype of type 'User'”。我尝试了不同的方式来访问该列表,但我想不出一种可行的方式。我已经包含了所有我能想到的相关代码,任何帮助将不胜感激!

集团Class

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 (_) { }
              }
            } 

州 Classes

class SwipeLoading extends SwipeState {}

class SwipeLoaded extends SwipeState {
  final List<User> users;

  const SwipeLoaded({
    required this.users,
});

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

事件 Classes

class SwipeLeftEvent extends SwipeEvent {
  final User user;

  SwipeLeftEvent({
    required this.user,
  });

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

我不完全确定这是否是您错误的根源,但对我来说有一点可疑的是这段代码:

class SwipeLoaded extends SwipeState {
  final List<User> users;

  const SwipeLoaded({
    required this.users,
  });

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

这里的 getter props 实际上返回了一个 List<List<User>>,我怀疑这可能不是你想要的。

这就是我认为您打算用于代码的内容:

class SwipeLoaded extends SwipeState {
  final List<User> users;

  const SwipeLoaded({
    required this.users,
  });

  @override
  List<User> get props => [...users];
}