Keys! What are they good for?
Article Summary
Emily Fortuna from Flutter reveals why most developers use Keys wrong—and when you actually need them. Spoiler: it's not as often as you think.
This deep-dive from the Flutter team explains Keys in Flutter development—a widely misunderstood feature that preserves state when widgets move in your tree. Through visual examples and code walkthroughs, it clarifies when Keys are essential versus unnecessary overhead.
Key Takeaways
- Keys only matter when modifying collections of stateful widgets with preserved state
- Place Keys at the top of widget subtrees, not on nested children
- ValueKey for unique values, ObjectKey for complex data, UniqueKey sparingly
- Stateless widget collections never need Keys—Flutter handles them automatically
- GlobalKeys enable cross-tree state access but often signal architectural issues
Keys preserve state during widget reordering, but only stateful widget collections need them—stateless widgets work fine without any Keys at all.
About This Article
Developers often put Keys on nested child widgets instead of at the top of the widget subtree. This causes Flutter to create new state instances and lose data consistency when collections are reordered.
Emily Fortuna shows the right way to place Keys by wrapping Padding widgets with Keys at the parent level. This lets Flutter's element-to-widget-matching algorithm preserve state correctly across tree reorganizations.
When Keys are placed at the subtree top level, stateful widgets maintain their state and show the expected visual changes when collections are reordered. The color-swapping tile example shows this working as it should.