{

How to find min and max value in Dart List? Flutter List| Dart By Example


Get Min and Max value from the list in dart or flutter

List contains multiple values, sometimes you need to get the minimum and maximum values from the given list.

Let’s see some programs to find the min and max values in different ways.

How to find the maximum and minimum value of a List of Numbers in the Dart program?

  • use Sort the list and get the first and last Element in Dart In this example, First sort the list of numbers using the sort() method. This sorts the numbers in the list in ascending order.

The first element becomes the minimum value and the last element is the maximum value. Here is an example dart code to find the minimum and maximum values using the sort

main() {
  List numbers = [8, 1, 0, 2, 102, 5];
  numbers.sort();
  print(numbers); // [0, 1, 2, 5, 8, 102]
  print(numbers[0]); //0
  print(numbers[numbers.length - 1]); //102
}
  • use reduce method

reduce method in a list used to reduce the collection of items into a single value using login inside a function.

Here is a Syntax.

 reduce(combinedFunction)

The combined Function holds the current value and next value and compares the values and assigns the current value with min or max logic.

Here is an example program

main() {
  List numbers = [8, 1, 0, 2, 102, 5];

  var maximumNumber =
      numbers.reduce((value, element) => value > element ? value : element);
  var minimumNumber =
      numbers.reduce((value, element) => value < element ? value : element);

  print(maximumNumber); // 102
  print(minimumNumber); // 0
}

How to get max and min values from a list of objects

In this, We have a list of employees, where each employee holds name and salary information.

this program finds the min and maximum salary of all employees listed in dart and flutter programming.

class Employee {
  final String name;
  final int salary;

  Employee(this.name, this.salary);
  @override
  String toString() {
    return 'Employee: {name: ${name}, salary: ${salary}}';
  }
}

final e1 = Employee('Erwin', 9000);
final e2 = Employee('Andrew', 70000);
final e3 = Employee('Mark', 8000);
final e4 = Employee('Otroc', 5000);

main() {
  List<Employee> employees = [e1, e2, e3, e4];

// Find Employee having minimum salary from all employees

  Employee employeeWithMinSalary = employees
      .reduce((item1, item2) => item1.salary < item2.salary ? item1 : item2);

  print(employeeWithMinSalary.toString()); // 102
  
  // Find Employee having Maximum salary from all employees

  Employee employeeWithMaxSalary = employees
      .reduce((item1, item2) => item1.salary > item2.salary ? item1 : item2);

  print(employeeWithMaxSalary.toString()); // 102
}

Output:

Employee: {name: Otroc, salary: 5000}
Employee: {name: Andrew, salary: 70000}

Conclusion

To summarize, Learned to find the min and max values of a list in dart and flutter programming.

  • List of primitive numbers
  • List of objects based on a numeric property
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart