这是我的示例 map :
Map<String, Transaction> _userTransactions = {
'one': Transaction(amount: - 45, date: DateTime.now(), title: 'socks', accountType: 'timr', notes: 'joa' , icon: Icon(Icons.today,), id: 'kololdcd', repeat: 'always'),
'two': Transaction(amount: - 60, date: DateTime.now(), title: 'present', accountType: 'timr', notes: 'brother' , icon: Icon(Icons.today,), id: 'kolofkfrcd', repeat: 'never'),
} ;
我想创建一个计算每日余额的函数。因此,我需要确定在参数日期中具有特定值的所有Transaction(并可能将它们放入新映射中),然后我需要从参数数量(从我们之前确定的有效Transaction)中访问值并加起来每日余额的所有值。
我是Futter的新手。我试图将 map 转换为列表,并使用函数。在哪里搜索日期数据并访问金额数据,但我感到困惑,因为我需要访问列表中的Widget交易中的值,因此对我来说事情有点复杂。
您对如何开始或如何编写整个方法有什么建议?
最佳答案
您可以将问题的文本直接转换为所需的代码。
int getDailyBalance(DateTime targetDate) {
// (Start with a list of all the values in the map)
return _userTransactions.values
// ...identify all Transaction that have a certain value for the argument date
.where((t) => t.date == targetDate)
// Then I need access to the values from the argument amount
.map((t) => t.amount)
// ...add up all values for the daily balance
.reduce((a1, a2) => a1 + a2)
// (Default to zero if no matching values are found [optional])
?? 0;;
}
关于javascript - 如何从 map 中的小部件搜索和访问数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243873/