I have the following code that sums cash for people with the same name using list fold.
void main() {
List<Map<String,dynamic>> people = [{'name': 'Jim', 'cash':44.86},{'name': 'Jim', 'cash':40.55},{'name': 'Bob', 'cash':10.99},{'name': 'Bob', 'cash':10.99}];
Map resultAmount = people.fold<Map<String, num>>({}, (totalMap, element) {
final String key = element['name'];
if (totalMap[key] == null) totalMap[key] = 0;
totalMap[key] += element['cash'].toDouble();
return totalMap;
});
print(resultAmount);
}
prints:
{Jim: 85.41, Bob: 21.98}
How can I get it to work with null-safety?