15

I have a list of string as List<String> temp = ['a', 'b', 'c', 'd']; I want to modify and remove specific item from the list and return new list without the removed item.

For example, if I want to remove index 2, what I want to get is ['a', 'b', 'd'] removeAt doesn't work since it just returns removed string item...

5 Answers 5

32

You can use cascade notation to return the list when you call removeAt.

void main() {
  print(['a', 'b', 'c', 'd']..removeAt(2));
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks - this is what I wanted. Do you know why cascade notation allows us to return original list instead removed string?
Cascade basically just returns the object you operate on, instead of the return value from the method call.
as someone who's currently learning Dart, I'm a little confused as to why this is necessary...? the List.removeAt() method directly modifies the list that it operates on.. so in the context of this question, after calling temp.removeAt(2), the list in temp quite literally is the "new list without the removed item". or (as a Dart noob) am I missing something here?
@AndreGreeff temp.removeAt(2) returns 'c', while temp..removeAt(2) returns ['a', 'b', 'd']. But yes, temp will store ['a', 'b', 'd'] regardless.
ah, so I guess the "difference" between the two in this case is mostly just convenience then..? thanks @mmcdon20
9

temp.removeAt(index); is exactly what you want.

void main() {
  List<String> temp = ['a', 'b', 'c', 'd'];
  temp.removeAt(2);
  print(temp);
}

this function prints ['a', 'b', 'd'] which is what you wanted to get right?

but if you can also get the removed value with this.

void main() {
  List<String> temp = ['a', 'b', 'c', 'd'];
  var removedValue = temp.removeAt(2);
  print(removedValue);
}

If what you want is to get a clone of the original list but without the element you can create a new one like this.

  void main() {
  List<String> temp = ['a', 'b', 'c', 'd'];
  int indexToRemove = 2;
  List newList = temp.where((x) => temp.indexOf(x) != indexToRemove).toList();
  print(newList);
}

Comments

3

If you want to remove items with logic you can also check out the code below this will help you to remove items with conditions.

List<String> temp = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];

for (var i = 0; i < temp.length; i++){
  if(temp[i] == "Mon") {
    temp.removeAt(i);
 }
}

Comments

1

If you don't want to modify initial list

final list = ['a', 'b', 'c', 'd'];
return List.from(list)..removeAt(2);

or

final list = ['a', 'b', 'c', 'd'];
return [...list]..removeAt(2);

Comments

0

This also might be helpful in some cases, specially when dealing with STATE from riverpod:

if (state.contains(foo)) {
      state = state.where((item) => item.id != foo.id).toList();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.