How to delete duplicates in a dart List? list.distinct()?

后端 未结 11 2110
夕颜
夕颜 2020-12-01 05:21

How do i delete duplicates from a list without fooling around with a set? Is there something like list.distinct()? or list.unique()?

void main() {
  print(\"         


        
11条回答
  •  执笔经年
    2020-12-01 05:43

    Using dart 2.3+, you can use the spread operators to do this:

    final ids = [1, 4, 4, 4, 5, 6, 6]; 
    final distinctIds = [...{...ids}];
    

    Whether this is more or less readable than ids.toSet().toList() I'll let the reader decide :)

提交回复
热议问题