我有一张对象 map :
Map<String, Transaction> _userIncome = {
'four': Transaction(amount: 450, date: DateTime.now(), title: 'Einkommen', accountType: 'timr', notes: 'joa' , icon: Icon(Icons.today,), id: 'kololdcd', repeat: 'always'),
'five': Transaction(amount: 60, date: DateTime.now(), title: 'Bruder', accountType: 'timr', notes: 'brother' , icon: Icon(Icons.today,), id: 'kolkfrcd', repeat: 'never'),
'six': Transaction(amount: 60, date: DateTime.now(), title: 'Rückerstattung', accountType: 'timr', notes: 'brother' , icon: Icon(Icons.today,), id: 'kolofkfrcd', repeat: 'never'),
};
如果我知道值(值是唯一的),我想获取 key ,以便我可以通过其键删除条目:
_userIncome.remove(key);
我可以用给定的值识别条目:
_userIncome.values
.where((t) => t.date.difference(datte).inDays == 0 && t.id == transactionId);
但我不知道如何获得匹配的 key ,以便删除条目。或者有没有办法通过它的值删除一个条目?
我很想听听一些建议:)
最佳答案
只需使用 removeWhere
直接删除条目。以下是示例:
_userIncome.removeWhere((k,t) => t.date.difference(datte).inDays == 0 && t.id == transactionId);
要获取索引(或更确切地说是条目),请执行以下操作:
final key = _userIncome.entries
.firstWhere(
(entry) => entry.date.difference(datte).inDays == 0 && entry.id == transactionId,
orElse: () => null,
)
?.key;
关于javascript - 如何通过在 flutter 中知道其值来删除 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60397861/