Search code examples
flutterdartwhere-clause

.Where() Against Another List


I have 2 lists. I would like to sort down the users list where it does not contain any users from the members list. I was thinking the where function would work here but Im not sure how to add a list to check against. Is there an optimal solution for something like this?

    void _sortMemberList(List<User> users) {
    if (members != null) {
      users = users.where((user) => user.id != /*check against all elements in members*/).toList();
    } 

    emit(state.copyWith(
        members: members,
        nonMembers: users,
      ));
  }

Solution

  • you can try:

    void _sortMemberList(List<User> users) {
      if (members != null) {
        users = users.where((user) => !members.any((member) => member.id == user.id)).toList();
      }
    
      emit(state.copyWith(
        members: members,
        nonMembers: users,
      ));
    }