Created
August 11, 2021 15:56
-
-
Save fladago/7a7379a7c2453965db767ada2f1f20da to your computer and use it in GitHub Desktop.
Maps in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
var nMap = <String, String>{ | |
//key //value | |
'🎯': '#dart', | |
'🏹': 'bow', | |
'🧵': 'thread' | |
}; | |
nMap['♥️'] = "#flutter"; | |
print(nMap['♥️']); | |
// List of keys | |
var keys = nMap.keys.toList(); | |
print(keys); // [🎯, 🏹, 🧵, ♥️] | |
// List of values | |
print(nMap.values.toList()); //[#dart, bow, thread, #flutter] | |
nMap.remove('🧵'); // put the key | |
print(nMap); //{🎯: #dart, 🏹: bow, ♥️: #flutter} | |
//Remove all values that do not divide by 2 without a remainder | |
var newMap = <String, int>{'one': 1, 'two': 2, 'four': 4, 'seven': 7}; | |
newMap.removeWhere((key, value) => (value % 2 != 0)); | |
print(newMap); // {two: 2, four: 4} | |
print(newMap.isEmpty); // false | |
print(newMap.isNotEmpty); // true | |
// first Map entry | |
print(newMap.entries.first); //MapEntry(two: 2) | |
print(newMap.entries.last); //MapEntry(four: 4) | |
var containsKey = nMap.containsKey('🎯'); | |
print(containsKey); //true | |
print(nMap.containsValue('#flutter')); //true | |
// Looping over elements of a map | |
//For | |
for (var item in nMap.entries) { | |
//You can use nMap.keys or nMap.values | |
print('Key: ${item.key} and Value: ${item.value}'); | |
} | |
//forEach | |
nMap.forEach((key, value) => print('$key : $value')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment