# 在小部件树中检测到重复的 GlobalKey：GlobalKey#abcde 被多个小部件使用

- **ID:** `flutter/global-key-duplicate-reparent`
- **领域:** flutter
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

同一个 GlobalKey 被分配给了同一小部件树中的多个小部件，通常是由于列表或条件渲染中不当的键重用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Flutter 3.10.0 | active | — | — |
| Dart 3.0.0 | active | — | — |

## 解决方案

1. ```
   Ensure each widget has a unique GlobalKey by generating a new key per instance. Example: ListView.builder(itemBuilder: (context, index) => MyWidget(key: GlobalKey(), data: items[index]));
   ```
2. ```
   Use ValueKey or ObjectKey instead of GlobalKey if state preservation is not required for every widget. Example: ListView(children: items.map((item) => MyWidget(key: ValueKey(item.id))).toList());
   ```
3. ```
   If using a GlobalKey for a form, store it in a map keyed by unique identifiers. Example: Map<String, GlobalKey<FormState>> formKeys = {};
   ```

## 无效尝试

- **Remove the GlobalKey entirely from all widgets** — GlobalKeys are often needed for state preservation or form validation; removing them may break functionality. (70% 失败率)
- **Use a static GlobalKey variable shared across widgets** — A static key is still a single instance; if used in multiple widgets, it will cause the same duplicate error. (80% 失败率)
