'How to get sum of firestore document values using fold? Flutter
body: StreamBuilder<QuerySnapshot>(
stream: record,
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
..........
..........
final totalIncome =
snapshot.data?.docs.fold<double>(0, (previousValue, element) {
return previousValue + (element['incomeAmount'] ?? 0.0);
}) as double;
...........
...........
This works if a field exists. If there is no field in firestore this gives an error field does not exist within the DocumentSnapshotPlatform
How can I make it work something like
element?['incomeAmount']
or if there is no field return 0?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
