📱 Flutter Intermediate

What is a Key in Flutter and when should you use it?

Why Interviewers Ask This

This tests whether you can apply Flutter knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

A Key is an identifier that helps Flutter's reconciliation algorithm correctly match widget instances across rebuilds, especially in lists. Without keys, when you reorder a list of StatefulWidgets, Flutter may incorrectly reuse State objects from the old positions, causing visual glitches or lost data. Use ValueKey(id) when identity is based on a unique value, ObjectKey(object) when the object itself is the identifier, and UniqueKey() when you want to force a widget to always be treated as new.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.