Using Keys in Flutter

If you are unaware of some terms like ValueKey, UniqueKey, ObjectKey etc then you on the right article.

A Key is an identifier for Widgets, Elements and SemanticsNodes.A new widget will only be used to update an existing element if its key is the same as the key of the current widget associated with the element.


MyTextField a simple stateful widget containing TextField
Home Page


A key that uses a value of a particular type to identify itself.A ValueKey<T> is equal to another ValueKey<T> if, and only if, their values are operator==.This class can be subclassed to create value keys that will not be equal to other value keys that happen to use the same value. If the subclass is private, this results in a value key type that cannot collide with keys from other sources, which could be useful, for example, if the keys are being used as fallbacks in the same scope as keys supplied from another widget.
children: <Widget>[
if (showFirst) MyTextField(key: ValueKey(1),),
MyTextField(key: ValueKey(2)),


A key that is only equal to itself.

Flutter | Vue | IIT Roorkee | Physics Educator at Unacademy | Artist | Flute Player | Love for machinary, bikes, tools | DIY person