Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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];
}

>Solution :

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];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading