๐ Keys trong Flutter
1. Key lร gรฌ?
Phแบงn tiรชu ฤแป โ1. Key lร gรฌ?โKey lร mแปt ฤแปnh danh duy nhแบฅt giรบp Flutter phรขn biแปt cรกc widgets trong Widget Tree. Khi khรดng cรณ key, Flutter dแปฑa vร o vแป trรญ vร kiแปu cแปงa widget ฤแป xรกc ฤแปnh widget nร o lร widget nร o khi rebuild.
Khรดng cรณ Key: Cรณ Key:โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ Widget A [0] โ โ Widget A [key1] โโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโคโ Widget B [1] โ โ Widget B [key2] โโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโคโ Widget C [2] โ โ Widget C [key3] โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ โFlutter dแปฑa vร o Flutter dแปฑa vร oINDEX ฤแป nhแบญn diแปn KEY ฤแป nhแบญn diแปn2. Tแบกi sao cแบงn Key?
Phแบงn tiรชu ฤแป โ2. Tแบกi sao cแบงn Key?โVแบฅn ฤแป khi khรดng cรณ Key
Phแบงn tiรชu ฤแป โVแบฅn ฤแป khi khรดng cรณ KeyโKhi thแปฉ tแปฑ hoแบทc sแป lฦฐแปฃng widgets thay ฤแปi, Flutter cรณ thแป nhแบงm lแบซn widget nร o lร widget nร o:
Trฦฐแปc khi xรณa Item B: Sau khi xรณa Item B:โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ Item A [0] โ โ Item A [0] โ โ Vแบซn ฤรบngโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโคโ Item B [1] โ โ โ Item C [1] โ โ Flutter nghฤฉ ฤรขy lร Item B!โโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโ Item C [2] โโโโโโโโโโโโโโโโโHแบญu quแบฃ: State cแปงa Item B cรณ thแป gรกn nhแบงm cho Item C!
Giแบฃi phรกp vแปi Key
Phแบงn tiรชu ฤแป โGiแบฃi phรกp vแปi KeyโTrฦฐแปc khi xรณa Item B: Sau khi xรณa Item B:โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโ Item A [keyA] โ โ Item A [keyA] โ โ Match by keyโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโคโ Item B [keyB] โ โ โ Item C [keyC] โ โ Match by key, ฤรบng state!โโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโ Item C [keyC] โโโโโโโโโโโโโโโโโโโโโ3. Cรกc loแบกi Key
Phแบงn tiรชu ฤแป โ3. Cรกc loแบกi Keyโ3.1 ValueKey
Phแบงn tiรชu ฤแป โ3.1 ValueKeyโDรนng khi bแบกn cรณ mแปt giรก trแป duy nhแบฅt ฤแป ฤแปnh danh:
ValueKey('user-123')ValueKey(item.id)ValueKey(email)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ValueKey<T> โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโคโ So sรกnh bแบฑng value.operator==() โโ โโ ValueKey(1) == ValueKey(1) โ โโ ValueKey('a') == ValueKey('a') โ โโ ValueKey(1) == ValueKey(2) โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ3.2 ObjectKey
Phแบงn tiรชu ฤแป โ3.2 ObjectKeyโDรนng khi bแบกn muแปn so sรกnh bแบฑng identity (cรนng object instance):
ObjectKey(userObject)ObjectKey(myInstance)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ObjectKey โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโคโ So sรกnh bแบฑng identical() โโ โโ obj1 = User('A') โโ obj2 = User('A') โโ ObjectKey(obj1) == ObjectKey(obj1) โโโ ObjectKey(obj1) == ObjectKey(obj2) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ3.3 UniqueKey
Phแบงn tiรชu ฤแป โ3.3 UniqueKeyโTแบกo key mแปi mแปi lแบงn (dรนng khi muแปn force rebuild):
UniqueKey() // Mแปi lแบงn gแปi tแบกo key khรกc nhauโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ UniqueKey โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโคโ Luรดn tแบกo key mแปi โโ โโ UniqueKey() != UniqueKey() โ โโ Sแปญ dแปฅng: Force widget rebuild โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ3.4 GlobalKey
Phแบงn tiรชu ฤแป โ3.4 GlobalKeyโKey cรณ thแป truy cแบญp tแปซ bแบฅt kแปณ ฤรขu trong app:
final formKey = GlobalKey<FormState>();// Sau ฤรณ: formKey.currentState?.validate()โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ GlobalKey<T> โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโคโ โข ฤแปnh danh duy nhแบฅt trong toร n bแป app โโ โข Cรณ thแป truy cแบญp State vร Widget โโ โข Tแปn performance - dรนng hแบกn chแบฟ! โโ โโ formKey.currentState โ Access State โโ formKey.currentWidget โ Access Widget โโ formKey.currentContext โ Access Context โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ4. Quyแบฟt ฤแปnh Key Selection Tree
Phแบงn tiรชu ฤแป โ4. Quyแบฟt ฤแปnh Key Selection Treeโ Cแบงn Key khรดng? โ โโโโโโโโโโโโโโดโโโโโโโโโโโโโ โ โ Danh sรกch Widget cแป ฤแปnh cรณ thแป thay ฤแปi khรดng ฤแปi thแปฉ tแปฑ โ โ CแบฆN KEY KHรNG CแบฆN KEY โ โโโโโโโโโดโโโโโโโโ โ โ Cรณ ID Khรดng cรณ ID unique? unique? โ โValueKey(id) ObjectKey(object) hoแบทc UniqueKey() (rare)5. Vรญ dแปฅ minh hแปa: Checkbox List Bug
Phแบงn tiรชu ฤแป โ5. Vรญ dแปฅ minh hแปa: Checkbox List BugโKhรดng cรณ Key (Bug!)
Phแบงn tiรชu ฤแป โKhรดng cรณ Key (Bug!)โInitial State: Sau khi xรณa "Apple":โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ [โ] Apple [0] โ โ [โ] Banana [0] โ โ Checkbox state cแปงa Apple!โโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโคโ [ ] Banana [1] โ โ โ [ ] Cherry [1] โ โ Checkbox state cแปงa Banana!โโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโ [ ] Cherry [2] โโโโโโโโโโโโโโโโโโโโโโโโ
Flutter giแปฏ state theo INDEX, khรดng theo item!Cรณ Key (ฤรบng!)
Phแบงn tiรชu ฤแป โCรณ Key (ฤรบng!)โInitial State: Sau khi xรณa "Apple":โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโ [โ] Apple [key: apple] โ โ [ ] Banana [key: banana]โ โ ฤรบng state!โโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโคโ [ ] Banana [key: banana]โ โ โ [ ] Cherry [key: cherry]โ โ ฤรบng state!โโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโโ [ ] Cherry [key: cherry]โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Flutter giแปฏ state theo KEY!6. Khi nร o dรนng Key?
Phแบงn tiรชu ฤแป โ6. Khi nร o dรนng Key?โโ Nรชn dรนng Key
Phแบงn tiรชu ฤแป โโ Nรชn dรนng Keyโ| Trฦฐแปng hแปฃp | Loแบกi Key |
|---|---|
| ListView.builder vแปi items cรณ thแป reorder/delete | ValueKey(item.id) |
| AnimatedList | ValueKey hoแบทc ObjectKey |
| Form validation | GlobalKey<FormState> |
| Access widget state tแปซ bรชn ngoร i | GlobalKey |
| Shuffle widgets vแปi animation | ValueKey |
โ Khรดng cแบงn Key
Phแบงn tiรชu ฤแป โโ Khรดng cแบงn Keyโ| Trฦฐแปng hแปฃp |
|---|
| Danh sรกch tฤฉnh khรดng thay ฤแปi |
| Widgets khรดng cรณ state |
| Thแปฉ tแปฑ widgets khรดng bao giแป thay ฤแปi |
7. Performance Considerations
Phแบงn tiรชu ฤแป โ7. Performance Considerationsโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Performance Impact โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโคโ โโ LocalKey (ValueKey, ObjectKey, UniqueKey) โโ โโโ Tแปt โ โโ โข So sรกnh nhanh โโ โข Chแป trong phแบกm vi parent โโ โโ GlobalKey โโ โโโ Tแปn performance โ ๏ธ โโ โข Cแบงn maintain registry toร n app โโ โข Chแป dรนng khi thแปฑc sแปฑ cแบงn โโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๐ Tรณm tแบฏt
Phแบงn tiรชu ฤแป โ๐ Tรณm tแบฏtโ| Key Type | So sรกnh bแบฑng | Use case |
|---|---|---|
ValueKey |
== (value equality) |
ID, string, number |
ObjectKey |
identical (same instance) |
Object reference |
UniqueKey |
Luรดn khรกc nhau | Force rebuild |
GlobalKey |
Registry lookup | Access state/widget |
Quy tแบฏc vร ng:
Dรนng Key khi thแปฉ tแปฑ hoแบทc sแป lฦฐแปฃng widgets cรณ thแป thay ฤแปi vร widgets cรณ state riรชng.