Last active
December 3, 2023 14:06
-
-
Save abdorll/6f5e328e07211468eb0740d137d1e50d to your computer and use it in GitHub Desktop.
Map in dart example
This file contains hidden or 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() { | |
// Creating a map of key-value pairs (int to String) | |
Map<int, String> studentData = { | |
3620301111: 'Ololade', | |
3620302222: 'Chukwuemeka', | |
3620303333: 'Umar', | |
}; | |
// Accessing values using keys | |
String ololadeName = studentData[3620301111]!; // 'Ololade' | |
// Adding a new key-value pair | |
studentData[3620304444] = 'Olumide'; | |
// Checking if a key exists in the map | |
bool isChukwuemekaPresent = studentData.containsKey(3620302222); // true | |
// Removing a key-value pair | |
studentData.remove(3620303333); | |
// Finding the number of key-value pairs | |
int numberOfPeople = studentData.length; // 3 | |
// Iterating through the map | |
studentData.forEach((matric, name) { | |
print('$matric belongs to $name'); | |
}); | |
// Maps can have keys and values of different types | |
Map<int, dynamic> person = { | |
3620305555: 'Chinonso', | |
3620306666: 29, | |
3620307777: false, | |
}; | |
// Creating an empty map and adding key-value pairs later | |
Map<int, double> emptyMap = {}; | |
emptyMap[3620308888] = 175.5; | |
// ================== MAP PROPERTIES | |
// 1. length → int (Returns the number of key-value pairs) | |
int mapLength = studentData.length; | |
// 2. isEmpty → bool (Returns true if the map is empty) | |
bool isMapEmpty = studentData.isEmpty; | |
// 3. isNotEmpty → bool (Returns true if the map is not empty) | |
bool isMapNotEmpty = studentData.isNotEmpty; | |
// 4. keys → Iterable<K> (Returns an iterable of the map's keys) | |
Iterable<int> personKeys = person.keys; | |
// 5. values → Iterable<V> (Returns an iterable of the map's values) | |
Iterable<dynamic> personValues = person.values; | |
// ================== MAP METHODS | |
// 1. putIfAbsent(key, ifAbsent) → V (Puts a key-value pair in the map if the key does not exist) | |
studentData.putIfAbsent(3620309999, () => 'Ngozi'); | |
// 2. update(key, update, {ifAbsent}) → V (Updates the value for the given key or adds a new key-value pair if the key does not exist) | |
studentData.update(3620302222, (value) => 'Chukwuemeka', ifAbsent: () => 'Chukwuemeka'); | |
// 3. clear() → void (Removes all key-value pairs from the map) | |
studentData.clear(); | |
// 4. removeWhere(predicate) → void (Removes key-value pairs that satisfy a condition) | |
person.removeWhere((key, value) => value is bool && !value); | |
// 5. containsValue(value) → bool (Checks if the map contains a specific value) | |
bool hasValue = studentData.containsValue('Chinwe'); | |
// 6. map() → Map<K2, V2> (Creates a new map by applying a function to each key-value pair) | |
Map<int, String> namesToGreetings = person.map((key, value) { | |
if (key == 3620305555) { | |
return MapEntry(key, 'Hello, $value'); | |
} | |
return MapEntry(key, value.toString()); | |
}); | |
// 7. clear() → void (Removes all key-value pairs from the map) | |
studentData.clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment