Dart| Flutter How to calculate the collection numbers sum example

Multiple ways to sum of numbers in Dart and flutter with examples

  • List reduce method
  • List fold method
  • lists sum with primitive numbers
  • Iterate list using map, and reduce to sum the object property

The collection is a collection of elements in dart.

For example, a List is one of the collections of items to store in insertion order.

Dart Collection of the sum of numbers example

There are multiple ways we can do the sum for the collection of numbers.

  • use List reduce method

List.reduce() method reduces the collection of elements with the iteration of elements and applies the function. Each element is iterated and adds to value, finally list is returned. Here, The Function addition of each element from the list.

int reduce(int Function(int, int) combine)

Here is an example

main() {

  List<int> lists = [1, 2, 3, 6, 8];
  var sum = lists.reduce((value, current) => value + current);

  print(sum);
  print(lists);
}

Output:

20
[1, 2, 3, 6, 8]
  • use the List fold method

List fold method is another way to reduce the collection into some function

T fold<T>(T initialValue, T Function(T, int) combine)

The first parameter is initialValue The second parameter is a combined function.

Here is an example program

main() {
  List<int> lists = [1, 2, 3, 6, 8];
  int sum = lists.fold(0, (previous, current) => previous + current);

  print(sum);
  print(lists);
}

Output:

20
[1, 2, 3, 6, 8]
  • Collection sum property The collection.dart module provides a sum property on a list object.

It is easy and quick to get a sum of a number.

Here is a program code

import 'package:collection/collection.dart';

main() {
  List<int> lists = [1, 2, 3, 6, 8];
  int sum = lists.sum;

  print(sum);
  print(lists);
}

Calculate the sum property value in a list of objects

This example finds the sum of employee salaries in a list of employee objects. Let’s create an employee object.

There are multiple ways we can do the sum of the object by the property value.

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

  Map<String, dynamic> toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

First Way, iterate the list using the mapπŸ”— method and apply reduceπŸ”— function to sum the current and previous value.

  int result = employees
      .map((item) => item.salary)
      .reduce((value, current) => value + current);

The second way, use the fold methodπŸ”— to iterate and sum the list of objects by property key

double result1 =
      employees.fold(0, (sum, element) => sum.toDouble() + element.salary);

Here is an example program

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

  Map<String, dynamic> toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

void main() {
  final e1 =
      Employee(11, 'Erwin', 9000, DateTime.parse("2020-06-21 00:00:00.000"));
  final e2 =
      Employee(21, 'Andrew', 70000, DateTime.parse("2021-07-23 00:00:00.000"));
  final e3 =
      Employee(4, 'Mark', 8000, DateTime.parse("2017-08-24 00:00:00.000"));
  final e4 =
      Employee(12, 'Otroc', 5000, DateTime.parse("2022-12-05 00:00:00.000"));

  final List<Employee> employees = [e1, e2, e3, e4];
  int result = employees
      .map((item) => item.salary)
      .reduce((value, current) => value + current);
  print(result); //92000
  double result1 =
      employees.fold(0, (sum, element) => sum.toDouble() + element.salary);

  print(result1); //92000
}