Flutter & Dart (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What are Keys in Flutter and when must you use them?
junior LevelflutterBasic
In Simple Words:
A Key is an identifier for Widgets, Elements, and Semantics nodes:
- Most widgets do not need keys.
- When Required: When modifying a collection of StatefulWidgets of the same type (reordering, inserting, or removing items from a list).
- Without keys, the Element tree matches by type, causing state to attach to the wrong widgets (e.g. checking item 1 checks item 2 when item 1 is deleted!).
- Types:
ValueKey,ObjectKey,UniqueKey,PageStorageKey,GlobalKey.