Flutter Tutorial
In Flutter, we’ve Keys associated with every widget we use and they’re optional. In this article, we’ll explore what do they do or why they exists? In which scenarios it makes more sense to use them.
Keys are used to identify a specific widget based on a specific value in a Widget tree. By identifying a widget, we can perform specific operations on it. For instance, this can help in managing the state of a widget while the Widget may be reordered in the Widget tree.
Another example would be that the Widget is placed on multiple screens while maintaining the same state. Element tree takes its Key reference from the Widget tree to maintain the state of the Widget before rendering it on the screen.
This way the use cases becomes limited for Keys. In general cases, we might even don’t require Keys.
So far so cool!
Consider a scenario where we’ve a screen with a Screen Counter Text shown at the top and we’ve two widgets of same type MyWidget.
On clicking on either of the widget A or B, it adds up and increments the Screen Counter Text on the screen and also increments their own counter which is part of each widget itself as…