Flutter报错:_ReorderableListViewChildGlobalKey被多组件重复使用
解决ReorderableListView出现Key重复的错误
你遇到的The key [_ReorderableListViewChildGlobalKey ValueKey#46dc3] was used by multiple widgets错误,本质是ReorderableListView的直接子组件存在重复的Key。虽然你给Dismissible设置了Key,但可能存在以下问题,对应解决方案如下:
检查数据源的唯一性
确保所有item.rungId是全局唯一的。可以在构建列表时打印每个item的rungId,排查是否有重复:List<Widget> _getListItems() => _items .asMap() .map((i, item) { print('第$i个item的rungId: ${item.rungId}'); return MapEntry(i, _buildTenableListTile(item, i)); }) .values .toList();如果发现重复,需要修正数据源,保证每个
rungId不重复。替换Key类型为ValueKey
将Dismissible的Key从Key(item.rungId)替换为ValueKey(item.rungId),这是更明确的基于值的Key实现,能避免Key类型导致的识别问题:Widget _buildTenableListTile(Rung item, int index) { return Dismissible( key: ValueKey(item.rungId), onDismissed: (direction) { setState(() { _items.removeAt(index); _removeditems.add(item); }); }, background: Container(color: Colors.red), child: Padding( padding: const EdgeInsets.all(20.0), child: Card(child: cardWithInfoPage(item, context, index)), ), ); }排查子组件的Key冲突
检查cardWithInfoPage方法中是否使用了与item.rungId重复的Key(比如误用索引作为Key),如果内部Widget的Key重复,也可能触发全局Key冲突。确保所有Widget的Key都是唯一的。
内容的提问来源于stack exchange,提问作者fluttergogo
相关产品推荐
相关产品推荐

