Maps (aka Hashes, Associative Arrays)
Key-value pairs are implemented in Dart by Map objects. Defining an options hash creates a Map.[4]
primitives/hash_map.dart | |
| var options = { |
| 'color': 'red', |
| 'number': 2 |
| }; |
As you’d expect, retrieving values from a Map is done with square brackets.
| options['number']; // 2 |
Map objects support the usual methods. This includes retrieving keys (the keys getter method) and values (the values getter) and iterating over the entire object with forEach.
| var options = { |
| 'color': 'red', |
| 'number': 2 |
| }; |
| options.forEach((k, v) { |
| print("$k: $v"); |
| }); |
| // number: 2 |
| // color: red |
Recipe 4 | The order of key-value pairs is not guaranteed in most classes that implement the Map interface (like ... |
Get Dart 1 for Everyone now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.