Explain the concept of Keys in Flutter.

Updated Apr 28, 2026

Short answer

Keys preserve state when widgets move around in the widget tree.

Deep explanation

Flutter uses the widget's Type and Key to determine if a widget should be updated or completely recreated. If you have a stateful list where items can be reordered or deleted, using a Key (like ValueKey or UniqueKey) ensures Flutter maps the state correctly to the new position.

Real-world example

A to-do list app where users can drag and drop items to reorder them. Keys keep the checkbox state associated with the right task.

Common mistakes

  • Using a `UniqueKey` inside a `build()` method. It generates a new key every frame, destroying the state entirely.

Follow-up questions

  • What is a GlobalKey?
  • When should you NOT use Keys?

More Flutter interview questions

View all →