Let's say I have a class CartItem
class CartItem {
final int amount;
CartItem({this.amount});
}
And two instances of CartItem in the cartItems list
CartItem itemOne = CartItem(amount: 10);
CartItem itemTwo = CartItem(amount: 25);
List<CartItem> cartItems = [itemOne, itemTwo];
What is the cleanest way to get cartItems amount total?
to Solve The argument type 'String' can't be assigned to the parameter type 'int' Error Simple Solution is You have to set data the variable to List type. Just Like Below Example. Now you can use the data variable name parameter with your index. Same as below.
Collection sum property collection.dart module provides sum property on a list object. It is easy and quick to get a sum of a number. 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.
Starting with Dart 2.6 you can use extensions to define a utility method on the List. This works for numbers (example 1) but also for generic objects (example 2). extension ListUtils<T> on List<T> { num sumBy (num f (T element)) { num sum = 0; for (var item in this) { sum += f (item); } return sum; } }
Let’s create an employee object. There are multiple ways we can do the sum of the object by the property value. First Way, iterate the list using the map method and apply reduce function to sum the current and previous value. The second way, use the fold method to iterate and sum the list of objects by property key
The collection is a collection of elements in dart. For example, a List is one of the collections of items to store with insertion order. There are multiple ways we can do the sum for the collection of numbers. List. reduce () method reduces the collection of elements with the iteration of elements and applies the function.
With fold
:
final total = cartItems.fold(0, (sum, item) => sum + item.amount);
EDIT: There is a long-running issue with generic methods in Dart that have a callback as a parameter where the type of the arguments cannot be inferred. This wasn't an issue in the past since the default value of a generic argument was dynamic
.
However, with the release of null-safety, the type system of Dart was shaken up, and now the default type is Object?
. This is a problem since you now have to ensure that the objects within the callback aren't nullable before you can add them, and the easiest way to do this is to bypass type inference and explicitly assign a generic type.
There are a few ways you can accomplish this:
// Pass generic type parameters manually
final total = cartItems.fold<int>(0, (sum, item) => sum + item.amount);
// Explicitly type parameters in the callback
final total = cartItems.fold(0, (int sum, item) => sum + item.amount);
// Explicitly type the returned value
final int total = cartItems.fold(0, (sum, item) => sum + item.amount);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With