Home > Software engineering >  what is the use of keys and const
what is the use of keys and const

Time:09-27

in every time I write a code in vscode editor. every time I get an error the use key in widgets and also giving me an alert that prefer to use const constructor but what is the real use of it

CodePudding user response:

Components such as appbars orcomponents which need not be updated when the state rebuilds are to be marked with const cosntructors. This practice is not mandatory, but it helps to improve the overall speed and performance of the app. Keys help dart to uniquely identify widgets and help to get datas out of forms. More about keys here and here

CodePudding user response:

Those widgets which not need to rebuild every time the build() is called, are defined with const keyword. This improves performance of the app, and is best practice to use const keyword with widgets which need no update. Talking about keys, keys are identifiers for widgets and help Flutter to distinguish between different widgets so that their respective state remain with them, instead of getting assigned to another similar widget.

  • Related