如何通过知道颤振中的值来删除密钥?

Geo*_*eev 5 javascript dart flutter

我有一张对象地图:

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'),
};
Run Code Online (Sandbox Code Playgroud)

如果我知道值(值是唯一的),我想获取密钥,以便我可以通过其键删除条目:

_userIncome.remove(key);
Run Code Online (Sandbox Code Playgroud)

我可以用给定的值识别条目:

_userIncome.values
    .where((t) => t.date.difference(datte).inDays == 0 && t.id == transactionId);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何获得匹配的密钥,以便我可以删除该条目。或者有没有办法通过其值删除条目?

我很想听听一些建议:)

Har*_*dia 4

只需使用removeWhere直接删除条目即可。以下是示例:

 _userIncome.removeWhere((k,t) => t.date.difference(datte).inDays == 0 && t.id == transactionId);
Run Code Online (Sandbox Code Playgroud)

要获取索引(或更确切地说条目),请执行以下操作:

final key = _userIncome.entries
      .firstWhere(
        (entry) => entry.date.difference(datte).inDays == 0 && entry.id == transactionId,
        orElse: () => null,
      )
      ?.key;
Run Code Online (Sandbox Code Playgroud)