Dart List min/max value

前端 未结 5 542
清歌不尽
清歌不尽 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:27

    Assuming the list is not empty you can use Iterable.reduce :

    import 'dart:math';
    
    main(){
      print([1,2,8,6].reduce(max)); // 8
      print([1,2,8,6].reduce(min)); // 1
    }
    

提交回复
热议问题