Skip to main content

Command Palette

Search for a command to run...

Keys – Giving your widgets an ID card

Because "the third widget from the left" is a terrible way to remember a name.

Updated
3 min read
Keys – Giving your widgets an ID card
M

I love explaining tech concepts, that seem like a nightmare!

Imagine a classroom where students arrive every day and take a seat. The teacher needs to keep track of everyone, but there’s a catch: the teacher has a very short memory.

🪑 The Problem: No Keys (Identification by Position)

The teacher decides: "Whoever is sitting in Chair #1 is Rahul."

  • The Swap: If Rahul and Priya swap seats, the teacher doesn't realize it.

  • The Confusion: The teacher looks at Priya in Chair #1 and thinks, "Wow, Rahul got a haircut and a new voice!"

Flutter works the same way. By default, it identifies widgets by their position in the tree.

  1. "First child? Okay, use the state from the previous first child."

  2. "Second child? Same thing."

If your list reorders, Flutter might attach Rahul’s "state" (like his half-finished exam paper) to Priya.

🪪 The Solution: With Keys (Identification by Identity)

Now, imagine every student wears an ID Card. Even if Rahul moves from Chair #1 to Chair #10, the teacher checks the ID and says: "Ah, there you are, Rahul."

A Key tells Flutter: "This widget is a specific instance—no matter where it moves in the tree, keep its state attached to it."

When This Actually Matters

If Flutter "guesses" the identity based on position and gets it wrong, you’ll see these bugs:

  • Scroll positions reset to the top.

  • TextFields suddenly contain text from a different row.

  • Animations glitch or restart halfway through.

  • Checked boxes stay checked even if the item they belonged to was deleted.

Keys prevent that.

Simple Example

Without key:

ListView(
  children: jobs.map((job) => JobTile(job)).toList(),
)

If the list reorders, Flutter matches by position.

State can jump to the wrong tile.


With key:

ListView(
  children: jobs.map((job) =>
    JobTile(
      key: ValueKey(job.id),
      job: job,
    )
  ).toList(),
)

Now Flutter matches by job.id.

Even if the list changes order:

  • Correct state stays with correct job.

Important Line (Lock This In)

BuildContext is not a node in the tree — it’s your pointer into the Element tree.

And keys help Flutter decide which Element your widget should attach to.


Choosing Your "ID Card" Type

Key TypeBest Use Case"The Vibe"
ValueKeyLists, Todo items, Data-driven rows."Identify me by my unique Database ID."
UniqueKeyForcing a fresh start / Resetting a widget."I'm brand new every time you see me."
GlobalKeyForm validation, accessing state from afar."The VIP pass that lets you find me from anywhere."

When You Don’t Need Keys

If:

  • Order never changes

  • Items never move

  • No state inside

You usually don’t need one.

The "Big Idea" to Lock In

Flutter doesn’t actually "remember" Widgets (they are blueprints that get thrown away). It remembers Elements (the actual "living" pieces of the UI).

Keys help Flutter decide:

“Reuse this Element?”
or
“Destroy it and create a new one?”


One Sentence to Remember

Keys don’t stop rebuilds — they protect identity.

That’s it.

Tip: If your widgets are Stateless and only display data passed in from a parent, Flutter usually handles reordering perfectly without keys. Keys are the "Bodyguards" for StatefulWidgets.

🔗 What’s Next

Flutter lied. Not everything is a widget.

🌐 Let's connect - Linkedin | Instagram | GitHub | Twitter