1. When should you use Keys in Flutter? Explain with examples of situations where Keys are necessary.
Keys are necessary when widgets of the same type can change position, be added, or removed from a list, and you need to preserve their state or identity. Without Keys, Flutter matches widgets by type and position, causing state to be associated with the wrong widget when the list order changes. For example, a list of stateful widgets with text fields will lose or mix up their state when items are reordered without Keys. Use ValueKey when widgets can be uniquely identified by a value like an ID: ListView.builder creates items with ValueKey(item.id). When you remove an item, Flutter knows which widget to remove and properly preserves state of remaining items. Use ObjectKey when object identity matters more than value, and UniqueKey to force Flutter to treat each widget instance as completely unique. Keys are also necessary for animations when animating widgets in and out of lists - without Keys, Flutter can't track which widget is which, causing animations to break. In stateful lists where items maintain local state (checkboxes, text inputs, expanded/collapsed states), Keys ensure state stays with the correct item during reordering, insertions, or deletions. Place Keys at the level where the list items are, not on the list itself. For example, in a ListView of Card widgets each wrapping a StatefulWidget, put the Key on the Card or on the StatefulWidget depending on where state needs to be preserved. GlobalKey is powerful but expensive - use it only when you need to access widget state from outside or preserve state across major tree restructuring. For most cases, ValueKey or ObjectKey are sufficient and more efficient. Common mistake: Forgetting Keys when using AnimatedList, or using Keys on stateless widgets that don't need them (no benefit). Understanding when and where to use Keys prevents subtle state bugs in dynamic UIs.