{

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


List contains multiple values, sometimes you need to get the minimum and maximum values from the given list. Get Min and Max value from list in dart or flutter

Let’s see some programs to find the min and max value 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.

First element becomes minimum value and last element is maximum value. Here is an example dart code to find minimum and maximum value using 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)

Combined Function holds current value and next value and compares the values assign 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 value 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

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example