Dart List min/max value

前端 未结 5 504
清歌不尽
清歌不尽 2021-02-01 12:12

How do you get the min and max values of a List in Dart.

[1, 2, 3, 4, 5].min //returns 1
[1, 2, 3, 4, 5].max //returns 5         


        
5条回答
  •  无人共我
    2021-02-01 12:41

    You can now achieve this with an extension as of Dart 2.6:

    import 'dart:math';
    
    void main() {
      [1, 2, 3, 4, 5].min; // returns 1
      [1, 2, 3, 4, 5].max; // returns 5
    }
    
    extension FancyIterable on Iterable {
      int get max => reduce(math.max);
    
      int get min => reduce(math.min);
    }
    

提交回复
热议问题