{

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: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

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

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example