Le keys in Flutter aiutano il framework a identificare correttamente e preservare i widget attraverso i rebuild — particolarmente importante per i widget stateful nelle liste che vengono riordinate, aggiunte o rimosse. Comprendere quando le keys sono necessarie previene bug sottili e confusi.
Cosa fanno le keys
During rebuilds, Flutter MATCHES new widgets to existing elements/state by POSITION and
TYPE. Usually this works fine. But when widgets of the SAME TYPE are reordered/added/
removed in a list, position-matching gets confused → state attaches to the WRONG widget.
→ KEYS give widgets a stable IDENTITY so Flutter matches them correctly across changes.
