Key giúp bảo tồn state của widget khi thứ tự danh sách con thay đổi.
- Không có key, Flutter khớp widget theo kiểu và vị trí, dẫn đến lẫn lộn state.
- Dùng
ValueKeycho giá trị đơn giản,ObjectKeycho đối tượng phức tạp. - Cần thiết cho
ListView.builder, animated lists hoặc drag-drop. - Không có key khi reorder danh sách
StatefulWidgetsẽ hoán đổi state của chúng.
Keys preserve widget state when the list of children changes order.
- Without keys, Flutter matches widgets by type and position, causing state corruption.
- Use
ValueKeyfor simple values,ObjectKeyfor complex objects. - Essential for
ListView.builder, animated lists, or drag-drop.