81

How to remove items on List<ReplyTile> with id = 001.. ?

final List<ReplyTile> _replytile = <ReplyTile>[];

_replytile.add(
    ReplyTile(
        member_photo: 'photo',
        member_id: '001',
        date: '01-01-2018',
        member_name: 'Denis',
        id: '001',
        text: 'hallo..'
    )
);
Denis Ramdan
  • 1,763
  • 3
  • 18
  • 35

6 Answers6

195

removeWhere allows to do that:

replytile.removeWhere((item) => item.id == '001')

See also List Dartdoc

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • What if we have List . Can we remove dynamic added widget ? – Harsh Bhavsar Jan 22 '19 at 14:00
  • 1
    @HarshBhavsar not sure what you mean. Have you tried? What problem did you run into? – Günter Zöchbauer Jan 22 '19 at 14:05
  • https://stackoverflow.com/questions/54310614/remove-index-wise-customwidget-from-listwidget-in-flutter – Harsh Bhavsar Jan 22 '19 at 14:38
  • 2
    what if item.id == '001' is asynchronous? – Alex Roy Jun 15 '20 at 07:10
  • How could this be async? Is `id` an async getter? Using https://api.dart.dev/stable/2.8.4/dart-async/Stream/Stream.fromIterable.html, https://api.dart.dev/stable/2.8.4/dart-async/Stream/where.html, https://api.dart.dev/stable/2.8.4/dart-async/Stream/asyncMap.html should work: `Stream.fromIterable(replytile).asyncMap((item) async => {"item": item, "id": await item.id }).where((m) => m.id != '001').map((m) => m.item)).toList()`. Not tested and you might need to add some type annotations. – Günter Zöchbauer Jun 15 '20 at 07:34
  • stunning thanks, you just saved me – Delmontee Sep 22 '21 at 12:00
  • FYI. If you're working with a List array, then it will be: `replytile.removeWhere((item) => item["id"] == '001')` – Rob.Kachmar Apr 21 '23 at 14:59
32

In your case this works:

replytile.removeWhere((item) => item.id == '001');

For list with specific datatype such as int, remove also works.Eg:

List id = [1,2,3];
id.remove(1);
Suz'l Shrestha
  • 887
  • 9
  • 9
20
//For removing specific item from a list with the attribute value
replytile.removeWhere((item) => item.id == '001') 

//Remove item by specifying the position of the item in the list
replytile.removeAt(2)   

//Remove last item from the list
replytile.removeLast()

//Remove a range of items from the list
replytile.removeRange(2,5)
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
7

This also works

_listofTaskUI.removeAt(_quantity);
SilenceCodder
  • 2,874
  • 24
  • 28
0

if you have a generic list

List<int> sliderBtnIndex = [];//Your list

sliderBtnIndex.remove(int.tryParse(index)); //remove
gsm
  • 2,348
  • 17
  • 16
0

Here the simple list functions with sample output

Consider the sample list

List sampleList = ["001", "002", "003"];

Here some add to list functions with output

//Add to list

  sampleList.add("004");
//Output: 001 002 003 004

//Add to specific index

  sampleList.insert(0, "005");
//Output: 005 001 002 003

Here the list remove functions with examples consider the same sample list

sampleList.removeWhere((item) => item.id == '003');
//Output: 001 002

//Remove item from secific index

  sampleList.removeAt(2);
//Output: 001 003

//Remove item from last

  sampleList.removeLast();
//Output: 001 002

//Remove items between range

  sampleList.removeRange(1, 2);
//Output: 002 003
Rahul Raj
  • 1,010
  • 9
  • 10