Keys in Flutter, Concept of Key in Widgets

The importance of using keys in Flutter is discussed here

  • The keys are the IDs of widgets. In addition to Stateless Widgets, all widgets have them.
  • Widgets are reused or rebuilt based on these values by the Element tree.
  • When no key is specified (as is the case most of the time), the widget type is used to determine the key.
  • They should only be used when the framework needs your assistance to identify which widget needs updating.
  • The majority of the time, keys are not necessary.
  • Keys are mainly used to maintain state, so if your widgets’ children are all stateless, no key is needed.
  • The use of keys can allow you to make some micro-optimizations.
  • Adding keys to ListTile widgets is necessary if you are reordering listview children.
  • You can use the key to reorder or add/delete widgets at the point in the widget tree where these operations are taking place.
  • valueKey:- Using a ValueKey, a simple value in the form of a string or integer can be used as a local key.
  • ObjectKey:- ObjectKey is useful if your widget displays more complex data than a single value.
  • UniqueKey:- The key has the advantage of giving you a unique ID every time you use it.
    It should be placed in the build method, however, if you use it. If you don’t do this, your widget will never have the same ID, so the Element tree will never find a match.
    If not, your widget’s ID will never match, so your Element tree will never be able to reuse it.
  • GlobalKey:- Since GlobalKeys are similar to global variables, they can be used to maintain state throughout your app.
    In many cases, it is preferable to use a state management solution.
  • Adding
  • Removing
  • Reordering
  • for Setting Page (Global Key)
  • for TODO APP(Value key)
  • for Random Tile design (Unique Key)
  • for data Scroll design (Page Storage key)
  • for Address Book (Object Key)
  • for Address Book (Object Key)

--

--

Mobile Application Developer | GDG Speaker | Technical Blogger at medium | community member at StackOverflow | Organizer @FlutterIndore

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
Shirsh Shukla

Mobile Application Developer | GDG Speaker | Technical Blogger at medium | community member at StackOverflow | Organizer @FlutterIndore