Maps as Key-Value Stores
Create maps and read values by key.
Maps as Key-Value Stores is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a Map Is
A Map stores data as key-value pairs. Each key acts like a label you use to look up its matching value. 🗺️
Creating a Map
Use curly braces with key colon value pairs. Dart reads the colons and knows this is a Map, not a set.
var ages = {'Ana': 30, 'Ben': 25};Empty Maps
Empty curly braces already mean an empty Map. You can also add types to make your intent crystal clear.
var scores = <String, int>{};Reading a Value
Look up a value with square brackets and its key. The key goes inside the brackets, just like an index.
var ages = {'Ana': 30};
print(ages['Ana']);Missing Keys Give null
Ask for a key that does not exist and you get null back, not an error. Always plan for that null.
var ages = {'Ana': 30};
print(ages['Zoe']);Adding and Updating
Assign with square brackets to set a value. If the key is new it is added, and if it exists it is overwritten.
var ages = {'Ana': 30};
ages['Ben'] = 25;Keys Are Unique
Each key in a Map appears only once. Setting the same key again replaces its value rather than duplicating it.
var m = {'a': 1};
m['a'] = 2;Removing a Pair
Call remove with a key to delete that pair. It returns the removed value, or null if the key was missing.
var m = {'a': 1, 'b': 2};
m.remove('a');Does It Have a Key
The containsKey method tells you whether a key exists before you try to read it.
var m = {'a': 1};
print(m.containsKey('a'));How Many Pairs
The length property reports how many key-value pairs the map currently holds.
var m = {'a': 1, 'b': 2};
print(m.length);Safe Defaults
Combine a lookup with the ?? operator to supply a fallback when a key is missing.
var m = {'a': 1};
var v = m['z'] ?? 0;Quick Check
You read a Map with a key that does not exist. What do you get?
Recap
Great job! A Map links unique keys to values for fast lookup, with easy add, update, and remove. Missing keys give null. 🎉
Frequently asked questions
Is the “Maps as Key-Value Stores” lesson free?
Yes — the full text of “Maps as Key-Value Stores” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Maps as Key-Value Stores”?
Create maps and read values by key. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Maps as Key-Value Stores” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.