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.


So in this code we have MyTextField as a simple stateful widget having TextField with its Text Controller as a field variable:

MyTextField a simple stateful widget containing TextField
Home Page


Here’s where the role of KEY comes in picture. We’ll discuss only three types of keys ValueKey, ObjectKey, UniqueKey.

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)),


Let’s see what’s UniqueKey:

A key that is only equal to itself.
  1. https://medium.com/flutter/keys-what-are-they-good-for-13cb51742e7d
  2. https://www.youtube.com/watch?v=kn0EOS-ZiIc

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store