Reputation: 1954
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..'
)
);
Upvotes: 90
Views: 179690
Reputation: 24177
It is sometimes more reliable to create a new list from an existing one. Modifying a list could create pass-by-reference errors. If a list is passed as a parameter, calling remove
might alter the list outside of your function thereby creating unexpected results and hard to find bugs.
To create a new list from an old one just filter the results
var newList = oldList.where((x) => x.id != id)
or use something like whereNot
in DartX
var newList = oldList.whereNot((x) => x.id == id)
That being said creating a new list would allocate more memory every time you create a new list. An alternative would be to create a new list before you modify it, for example:
var safeList = [...oldList];
safeList.removeWhere((item) => item.id == '001')
Upvotes: 1
Reputation: 1503
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
Upvotes: 1
Reputation: 15699
//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)
Upvotes: 25
Reputation: 2426
if you have a generic list
List<int> sliderBtnIndex = [];//Your list
sliderBtnIndex.remove(int.tryParse(index)); //remove
Upvotes: 0
Reputation: 1107
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);
Upvotes: 35
Reputation: 658057
removeWhere
allows to do that:
replytile.removeWhere((item) => item.id == '001')
See also List Dartdoc
Upvotes: 214