Dart List min/max value

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

    For empty lists: This will return 0 if list is empty, the max value otherwise.

      List x = [ ];  
      print(x.isEmpty ? 0 : x.reduce(max)); //prints 0
    
      List x = [1,32,5];  
      print(x.isEmpty ? 0 : x.reduce(max)); //prints 32
    

提交回复
热议问题