Convert List<User> to Map<String, User> keyed by user id
Tests Dart collection-to-map idioms. A strong answer picks the for-element literal {for (var u in users) u.id: u}, mentions Map.fromIterable as a verbose alternative, and flags duplicate-key overwrites. Red flag: manually looping to populate an empty map.
WHAT THIS TESTS: This question checks whether you reach for modern Dart idioms when converting collections, or whether you fall back to imperative loops. At the senior level it is not enough to produce a correct result; the interviewer wants to see that you know the language evolved and that you prefer terse, type-inferred expressions where they improve readability.
A GOOD ANSWER COVERS: First, the candidate should lead with the for-element map literal, writing something like final userMap = {for (var user in users) user.id: user}; because this is the current community and language-team preference. Second, they should acknowledge Map.fromIterable as a working alternative, for example Map<String, User>.fromIterable(users, key: (u) => u.id, value: (u) => u), but immediately note that the Dart documentation itself calls the literal generally preferable because it allows more precise typing. Third, they should mention behavior with duplicate keys, explaining that the resulting map is a LinkedHashMap and that the last occurrence of a duplicate id overwrites any previous value without throwing. Fourth, they should briefly discuss efficiency, noting that both approaches are O of n single-pass operations and that there is no meaningful performance difference, so readability should drive the choice.
COMMON WRONG ANSWERS: The biggest red flag is writing an explicit for loop, creating an empty map, and then assigning entries one by one with map[user.id] = user. This works but signals that you are not comfortable with Dart's collection-for syntax. Another red flag is using Map.fromIterable without generic type arguments, which forces the compiler to lose type safety and can lead to runtime cast errors. A third warning sign is claiming that duplicate keys will cause a crash or exception; Dart maps silently overwrite on key collision.
LIKELY FOLLOW-UPS: An interviewer might ask what happens if two users share the same id, or how you would detect duplicates instead of silently losing data. They might also ask how to convert to a Map<String, String> mapping ids to names rather than ids to User objects, which tests whether you understand how to change the value expression in the literal. A third follow-up could be to implement the conversion in a way that throws if duplicate ids exist, which would require folding or an explicit loop with a containsKey check.
ONE CONCRETE EXAMPLE: Suppose you have a list of three users where the first two both have id seven and the third has id nine. Using final map = {for (var u in users) u.id: u}; produces a LinkedHashMap with two entries. The entry for key seven contains the second user because the second assignment overwrote the first, and the entry for key nine contains the third user. If you instead wanted to preserve the first user and throw on duplicates, you would need to write an imperative loop that checks if the map already contains the key before inserting.
Read the original → api.dart.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.