'list_remove_unrelated_type - dart

I have a function like this:

void function(List<int> listToRemove) {
  final List<int> list = [1, 2, 3, 4, 5];
  list.remove(listToRemove);
}

and its Invocation of remove with references of unrelated types. dart(list_remove_unrelated_type) error on list.remove(listToRemove).

I read https://dart-lang.github.io/linter/lints/list_remove_unrelated_type.html this but still dont know how to initialize listToRemove. Anyone have idea?



Solution 1:[1]

You have a List<int>, so you can use remove to remove an int from this list.
You're trying to remove a List<int> from a List<int> which is why it throws a list_remove_unrelated_type:
"DON'T invoke remove on List with an instance of different type than the parameter type."

If you want to remove List<int> from another List<int> you can try this:


void function(List<int> list, List<int> toRemove) {
  for(int item in toRemove){
    list.remove(item); // Removes the first occurrence of value from this list.
  }
}

void main() {
  List<int> list = [1, 2, 3, 4, 5];
  List<int> toRemove = [4, 1];
  function(list, toRemove);
  print(list); // [2, 3, 5]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ethicnology