Dart List min/max value

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

    If you don't want to import dart: math and still wants to use reduce:

    main() {
      List list = [2,8,1,6]; // List should not be empty.
      print(list.reduce((curr, next) => curr > next? curr: next)); // 8 --> Max
      print(list.reduce((curr, next) => curr < next? curr: next)); // 1 --> Min
    }
    

提交回复
热议问题