key property
Controls how one component replaces another component in the tree.
If the runtimeType and key properties of the two components are
operator==, respectively, then the new component replaces the old component by
updating the underlying element (i.e., by calling Element.update
with the
new component). Otherwise, the old element is removed from the tree, the new
component is inflated into an element, and the new element is inserted into the
tree.
In addition, using a GlobalKey
as the component's key allows the element
to be moved around the tree (changing parent) without losing state. When a
new component is found (its key and type do not match a previous component in
the same location), but there was a component with that same global key
elsewhere in the tree in the previous frame, then that component's element is
moved to the new location.
Generally, a component that is the only child of another component does not need an explicit key.
See also:
- The discussions at
Key
andGlobalKey
.
Implementation
final Key? key;