Search code examples
flutterdartstatefulwidget

What's the scope of an UniqueKey in flutter?


I'm an unique person. I mean I'm different from all other persons in this world. When I create an UniqueKey in flutter, what's the world in which it's different from all the others?


Solution

  • A UniqueKey is unique in exactly the same way that each instantiated object is a separate object. There isn't anything magical about the implementation of UniqueKey. A UniqueKey will never compare equal to another UniqueKey in the process, and it doesn't make sense to compare UniqueKeys across processes.

    A UniqueKey is not a UUID/GUID; if you ran, say, identityHashCode on it and saved it, there would be no guarantee that some other object wouldn't generate the same identityHashCode value. (And identityHashCode is still ultimately a hash code, which means that two objects might generate the same hash code anyway.)

    (If you're wondering if, say, a UniqueKey could be identical to a UniqueKey that was already created and garbage-collected in the process, then that'd be like a Zen koan: there'd be no way to tell since comparing it the earlier instance would require a reference to it, and a reference would have prevented it from being garbage-collected.)