Home > Software engineering >  Error: type 'List<User>' is not a subtype of type 'User' in Flutter, using
Error: type 'List<User>' is not a subtype of type 'User' in Flutter, using

Time:11-25

I am new to flutter and Bloc and I'm trying to implement a dating app that involves swiping users to like/dislike. I am using Bloc v8.0.1 and I am having difficulty in the Bloc Class in the event that a user swipes. I would like to remove the user from the list. Currently, I am receiving the error 'type 'List < User>' is not a subtype of type 'User'' in the emit(SwipeLoaded(users: List.from(state.props)..remove(event.user))); line of code. I have tried different ways to accesss the list but I can't figure out a way that works. I have included all the relevant code I can think of, any help would be greatly appreciated!

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

States Classes

class SwipeLoading extends SwipeState {}

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

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

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

Events Classes

class SwipeLeftEvent extends SwipeEvent {
  final User user;

  SwipeLeftEvent({
    required this.user,
  });

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

CodePudding user response:

I'm not entirely certain if this is the source of your error, but one thing that looks suspicious to me is this code:

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

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

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

The getter props here is actually returning a List<List<User>> which I suspect may not be what you intended.

This is what I think you intended for the code:

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

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

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