Sub topic 7
Putting the Key on the Right Widget
What you should already know
This chapter assumes that you understand Dart classes, StatelessWidget, StatefulWidget, setState, ListView, and ListTile. It also assumes that a key helps Flutter match widgets between rebuilds.
The important detail is where that match happens. Flutter compares the children of the same parent. Therefore, a key must be on the widget that represents one complete item at that parent level.
Key the item root
Consider a list whose rows contain local state, such as a tap counter. The widget returned for each item is the item root:
ListView.builder(
itemCount: contacts.length,
itemBuilder: (context, index) {
final contact = contacts[index];
return ContactRow(
key: ValueKey(contact.id),
name: contact.name,
);
},
)Here, each ContactRow is a direct child in the list's item structure. The key tells Flutter that the row for contact 42 is still the row for contact 42, even if its position changes.
The key should identify the data item, not its current index. An index describes a position. When items are inserted, deleted, or reordered, positions change.
Include stateful wrappers
A collection item often has a wrapper such as Dismissible, Draggable, or a custom animation widget. The wrapper may also own state. In that case, put the key on the wrapper:
ListView.builder(
itemCount: contacts.length,
itemBuilder: (context, index) {
final contact = contacts[index];
return Dismissible(
key: ValueKey(contact.id),
background: const ColoredBox(color: Colors.red),
onDismissed: (_) => removeContact(contact.id),
child: ContactRow(name: contact.name),
);
},
)The Dismissible is the topmost widget for the item. Its key lets Flutter preserve the entire item subtree, including the ContactRow below it.
Putting the key only on the inner row is weaker:
return Dismissible(
key: ValueKey(index),
child: ContactRow(
key: ValueKey(contact.id),
name: contact.name,
),
);The inner key cannot repair a wrong key on the wrapper. If the wrapper uses the index, the wrapper can keep the wrong dismissal or animation state after a reorder.
The same rule applies to layout wrappers:
List parent
└── keyed item root
└── item content
└── stateful descendantsA key on a descendant identifies that descendant among its own siblings. It does not identify the whole item to the parent list.
Compare common placements
| Placement | What Flutter can match | Typical result |
|---|---|---|
| Key on item root | The complete item subtree | Preferred for dynamic collections |
| Key on an inner child | Only that child's siblings | Outer wrapper state may be mismatched |
| Key on an index | A changing position | State follows positions, not items |
| No key | Same-type widgets by position | Reordering can mix up local state |
A useful mental model
Suppose a list changes from A, B, C to C, A, B. Flutter examines the widgets that are children of the list's item parent. If those item-root widgets have ValueKey('A'), ValueKey('B'), and ValueKey('C'), Flutter can find each old element by key and move its existing subtree.
Without keys, Flutter usually matches the first old child with the first new child, the second with the second, and so on. The State object remains attached to the position while the displayed data changes.
Exercise
Exercise
A list item is returned as AnimatedContainer containing CounterTile. Where should ValueKey(item.id) go if AnimatedContainer is the item root?
Show answer
Put the key on AnimatedContainer. It is the topmost widget returned for that item, so Flutter can preserve both the wrapper and the CounterTile subtree.