2

I want to remove an item from my list in a ListView.builder but it's saying it's read only.

List<Map> entries = [{'date': '2019-08-10', 'data': 85.0}, {'date': '2019-08-14', 'data': 84.0}];

onPressed: () {
  removeItem(index);
}

void removeItem(index) {
  entries.removeAt(index);
}

Another exception was thrown: Unsupported operation: read-only

This does not work either:

onPressed: () {
  removeItem(date);
}

void removeItem(date) {
  entries.removeWhere((item) => item['date'] == date);
}
2
  • If you change List<Map> to something else say List<int> entries = [1,2], do you still get the error? Commented Aug 14, 2019 at 12:17
  • @CopsOnRoad It seems not, no error if it's just a list of ints. Commented Aug 14, 2019 at 12:22

2 Answers 2

5

I got the solution from this, the link

entries = List.from(entries)..removeAt("theIndexValue");
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of updating the list, you can generate a new list with the where or the whereNot methods, something like this:

List<Map> updatedEntries = entries.whereNot((item) => item['date'] == date);

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.