📱 Flutter Intermediate

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

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.