You could use the list as you do and as cutch
mentioned manually add your own method to verify duplicates and sorting similar to the below.
import 'dart:html';
var someList = new List<String>();
String newItem = '';
void main() {
newItem = 'item 3';
if(!itemExistsInList(someList, newItem)){
someList.add(newItem);
sortList(someList);
}
// expected item 3
print(someList);
newItem = 'item 1';
if(!itemExistsInList(someList, newItem)){
someList.add(newItem);
sortList(someList);
}
// expected item 1, item 3
print(someList);
newItem = 'item 3';
if(!itemExistsInList(someList, newItem)){
someList.add(newItem);
sortList(someList);
}
// expected item 1, item 3. Same as previous as secondary item 3 was not added
print(someList);
}
// returns true if the specified item already exists in the specified list
// otherwise false
bool itemExistsInList(List<String> list, String item){
return list.some((v) => v.indexOf(item) != -1);
}
// sorts the list
void sortList(List<String> list){
list.sort((a, b) => a.compareTo(b));
}
You don't need to call the sortList()
function every time you add, I only did this for demonstration. Only calling it when you actually need to is enough off course.
.sort()
, .some()
and .indexOf
are explained in more detail under the collections section of the DART library documentation