Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created July 6, 2018 23:25
Show Gist options
  • Save graphicbeacon/b738e69b9985a5c11a841c94de1a1261 to your computer and use it in GitHub Desktop.
Save graphicbeacon/b738e69b9985a5c11a841c94de1a1261 to your computer and use it in GitHub Desktop.
Sample code for "Top 10 Map/Object utility methods you should know (Dart) 🎯" (Complete solution)
void main() {
var user = {
"firstName": "Tom",
"age": 25,
};
// 1. addAll()
user.addAll({
"lastName": "Smith",
"age": 26,
});
print(user);
print('\n-----\n');
// 2. containsKey()
print(user.containsKey("age"));
print(user.containsKey("accessToken"));
print('\n-----\n');
// 3. containsValue()
print(user.containsValue("Smith"));
print(user.containsValue(40));
print('\n-----\n');
// 4. forEach()
user.forEach((key, value) => print('Key: $key, Value: $value'));
print('\n-----\n');
// 5. putIfAbsent()
user.putIfAbsent("accessToken", () => "abf329jklr90rnlk2...");
print(user);
print('\n-----\n');
// 6. remove()
var removedToken = user.remove("accessToken");
print(removedToken);
print('\n-----\n');
// 7. removeWhere()
user.removeWhere((key, value) => key == "lastName");
print(user);
print('\n-----\n');
// 8. clear()
user.clear();
print(user);
print('\n-----\n');
// 9. update()
user["age"] = 25;
user.update("age", (dynamic val) => ++val);
// 10. Map.from()
var userCopy = Map.from(user);
print(userCopy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment