I came across this scenario this week again.
I’ve been using key: ValueKey(*aValue*)
but for no special reason, taking into consideration that probably using key: Key(*aValue*)
could work similarly.
Any top reason or something for a deep understanding of using which in any case?
3
Answers
The
Key
class is used to uniquely identify a widget in the widget tree. TheValueKey
subclass ofKey
is used when the key needs to be based on the value of an object rather than the object identity itself.In general, it is recommended to use
ValueKey
when the key needs to be based on the value of an object. This is becauseValueKey
uses the object’shashCode
and==
methods to determine the key, which ensures that the key remains the same even if the object itself is recreated with different attributes. This is important for cases where the widget tree needs to be rebuilt due to state changes, because usingValueKey
ensures that the corresponding widgets are updated correctly.On the other hand, if the key only needs to be based on the object’s identity, then using
Key
would be sufficient. This is typically the case when creating a new widget that has a different state than the previous widget.In summary, use
ValueKey
when the key needs to be based on the value of an object, and useKey
when the key only needs to be based on the object’s identity.Key
is abstract class so you can’t initiate it directly. There are few classes which inherits key.LocalKey (Another abstract class which extends Key). You can’t initiate it directly as well. LocalKey is a key which is not global.
UniqueKey – Local key used when you want to rebuild a widget everytime there is build called).
ObjectKey – Local key used to identify items based on object which was passed. It uses identity comparison to see if keys are equals. So if between builds if object reference is same, keys will be same. If they have different reference, build method will be called.
ValueKey (Local key used to identify different items based on their Value in widgets which has similar type of children like List/Row/Column). You can pass item.id as valueKey or entire item.It becomes more important if children are stateful widgets.
GlobalKey (A key that is unique across entire app). It should not be re-created every build. You can’t have same GlobalKey with more than one widget which leads to collision.
LabeledGlobalKey – Global key with debug level.
GlobalObjectKey – Same as ObjectKey but at global level. Hence you can access it anywhere.
its ok if you want to write different
Key
manually, but if you want to create it dynamically, then there is some problem :we can use
Key
like this:but
Key
only acceptString
, what if there is product with same name and different on price? dart not happy and telling you : "duplicate of key".then to make dart happy we can use the
ValueKey
:maybe some guy create a difference in their reference and add to our
myMillionListOfProduct
.BUT, again there is one product with same value,
Product(name: soap, price: 10.00)
, usingValueKey
will make dart not happy again.then we use
ObjectKey
instead.what if this guy add 1 more of his product with same reference?
then dart will tell you that the widgets have the same objectKey. if you want this to happen,
use
UniqueKey
instead.