How to get Map keys by values in Dart?

前端 未结 6 1505
小蘑菇
小蘑菇 2021-02-05 00:43

In Dart language how to get MAP keys by values?

I have a Map like;

{
  \"01\": \"USD\",
  \"17\": \"GBP\",
  \"33\": \"EUR\"
}

And I n

6条回答
  •  礼貌的吻别
    2021-02-05 01:29

    There is another one method (similar to Günter Zöchbauer answer):

    void main() {
    
      Map currencies = {
         "01": "USD",
         "17": "GBP",
         "33": "EUR"
      };
    
      MapEntry entry = currencies.entries.firstWhere((element) => element.value=='GBP', orElse: () => null);
    
      if(entry != null){
        print('key = ${entry.key}');
        print('value = ${entry.value}');
      }
    
    }
    

    In this code, you get MapEntry, which contains key and value, instead only key in a separate variable. It can be useful in some code.

提交回复
热议问题